Possible Duplicate:
Variable in JavaScript callback functions always gets last value in loop?
I am not able to pass the value of k to the callback function of fadeOut function.
my loop is as given below.
for(var k=0;k<image1.length;k++)
{
$(img[k]).fadeOut(200,function(k) {
alert(k);
$(this).attr('src', image2[k]);
$(this).fadeIn(200);
});
}
The jQuery
fadeOutfunction takes a callback function with no arguments. From the jQuery documentation, “The callback is not sent any arguments”. If you want to capture the value ofk, do something like the following: