I have the following code:
<div id="0">click here</div>
<div id="1">click here</div>
<div id="2">click here</div>
for(var i=0;i<3;i++){
document.getElementById(i).addEventListener("click",function(){
alert(i);
});
}
How can I get my callback function to alert 1, then 2 and then 3? It always alerts 3, and I need a way for it to save the value of 'i' at each iteration.