I have a loop that sends ajax requests. I want to include the loop index in my ajax callback function:
for (i=0;i<10;i++) {
$.ajax({
data: "index="+i
success: function (data) {
//I want to be able to see the variable (i) here
//since the request is async, it returns the last index on all
$("#div"+i).append(data);
}
})
}
You’ll need to wrap it up in a closure. This should do it: