This is a classic issue, you just have to use another closure: for (var i = 0; i < img_list.length;i++) (function(i){ $(new Image()).load(function () { ClassName.CallBackFunction.apply(this,[$(this),i]); }).attr("src",$(img_list[i]).attr("src")); })(i)
but it's a bit easier with $.each: $.each( img_list, function(i){ $(new Image()).load(function () { ClassName.CallBackFunction.apply(this,[$(this),i]); }).attr("src",$(img_list[i]).attr("src")); }) A short description about what is really happening: http://www.mennovanslooten.nl/blog/post/62 On Oct 18, 7:46 am, GTakacs1976 <[EMAIL PROTECTED]> wrote: > This is my code: > var ClassName = {}; > ClassName.CallBackFunction = function(index,i) { > alert($(index).attr('src') + ' ' + i); > > } > > function() { > for (var i = 0; i < img_list.length;i++){ > $(new Image()).load(function () { > ClassName.CallBackFunction.apply(this,[$(this),i]); > }).attr("src",$(img_list[i]).attr("src")); > } > > However when the alert comes up it always shows the same value for i, not > the value that was in effect at the time the load function was called. > > If I change my code to: > function() { > for (var i = 0; i < img_list.length;i++){ > $(new Image()).load( > ClassName.CallBackFunction.apply(this,[$(this),i]) > ).attr("src",$(img_list[i]).attr("src")); > } > > I get the correct value for "i" but now the $(this) points to the wrong > item. > > How can I have both the $(this) and the value of "i" passed to the callback > function? Or is it a "having my cake and eating it too" situation? > -- > View this message in > context:http://www.nabble.com/trouble-with-passing-parameters-to-a-callback-f... > Sent from the jQuery General Discussion mailing list archive at Nabble.com.