.click() callback references local variable from the calling method instead of copying by value
- by Eric Freese
The following jQuery Javascript code is included on an otherwise empty page.
$(function() {
for (var i = 0; i < 10; i++) {
element = $('<div>' + i + '</div>');
element.click(function() {
alert(i);
});
$('body').append(element);
}
});
The desired behavior is that this code should generate 10 div elements…