Consider the following example:
function async_callback(array1, array2, array3) {
// All arrays are equal length
for(var i = 0; i < array1.length; i++) {
var myElement = new Element('div', { 'id': 'dvMy' + i, 'events': { 'click': function() { SomeFunction(array1[i], array2[i], array3[i] } }});
$(document).appendChild(myElement);
}
}
Now when I click my element, I get a null value for all three arguments.
I even tried doing:
myElement.onclick = SomeFunction; // but this won't allow arguments
I know I can create a string and use eval() and that does work, but I don’t like eval().
Any ideas?
BTW: This is a simple example to replicate the problem, and not actual code.
This is your handler:
By the time its executed, the loop will have completed and
iwill therefore be equal to thelengthof the array, as per the conditional statement (i < array1.length) and the iteration statement (i++).The easiest way to resolve this is to wrap the handler in an additional immediately-executed function, and pass
ito it, — this will have the effect of retaining the value ofiin a new closure: