What am doing is attaching event on a class using a loop and index values are being used in the event handler code. Here is my code:
var classElements=document.getElementsByClassName("a");
for(var i=0; i<4; i++)
{
classElements[i].onClick=function(){
alert("Clicked button : "+i);
}
}
Whenever I click any of the buttons, it alerts:
Clicked Button : 4
What could be the problem?