Possible Duplicate:
Closures in a for loop and lexical environment
I am learning Closures in JavaScript... I saw example of simple code :
for (var i = 0; i < 10; i++) {
document.getElementById('box' + i).onclick = function() {
alert('You clicked on box #' + i);
};
}
But what exactly happens is that no matter what div
you select you get an alert about the last i
- last iteretion.
I saw an solution to that problem with inner function, but why is this happens? doest it's not binding the onclick
event on every iteretion?