[jQuery] trouble with passing parameters to a callback function
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-function-tp20044360s27240p20044360.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.