Possible Duplicate:
Javascript: closure of loop?
Pseudo javascript:
for (i, i<10, i++) {
new element.addEvent('click', function(){ alert(i) }).inject(dom)
}
When the loop finishes all the onclick events will trigger alerts with the 'final' value of i. What is the 'correct' way of having them alert the value of i as it was when the onclick event was added?
When I say correct I mean that I'm aware there are several ways of achiving this behavior, but I want to know the standard (ie expected by people that may encounter the code) way. Thanks.