Skip to content
Advertisement

JavaScript auto-incrementing a variable in jQuery and AJAX

I have JavaScript using jQuery and AJAX which creates a dynamic array, which has some values used for AJAX request as below;

<script type="text/javascript">
var array = Array("y","y","x","y","y","y");
function updateBackground(cellId, titleId) {
    var i = 0;
    $.ajax({
        type: "POST",
        url: "ajax.php",
        data: {
          filename: Array(array[i], "testdata", $("#"+titleId).html())
        },
        success: function(response){
          $("#"+cellId).css("background-image", "url('pdfthumb/" + response + "')");
        }
    });
i++;
}
</script>

The script is suppose to submit values in the array in array[i] for each AJAX request. I made a variable var i which auto increments.. But the script is not working.. The script works well if array[i] is replaced by array[0] or array[1] etc..

How can I solve the syntax error?

Advertisement

Answer

I fixed it… Thank you so much @Jed, @Pointy, @Crozin, and @Lord Vader for helping me to figure it out…. 🙂

I just take var i = 0; outside the loop…. above var array like;

var i = 0;
var array = Array("y","y","x","y","y","x");
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement