I am dynamically creating buttons with a on click event the following way:
//add button
for(i=1;i<=narrow+1;i++){
var btn = document.createElement("BUTTON");
btn.id="element"+i;
var t = document.createTextNode("3D View");
btn.appendChild(t);
btn.style.position="absolute";
btn.style.top="520px";
btn.style.left=100+120*(i-1)+"px";
btn.addEventListener('click', function(){window.alert(i-1+" "+nmol[i-1]);});
The buttons created are fine but the argument in the function of the addEventListener event seems to no increment at all. When printed i stayed to value 1.
Anyone can explain me why?
Thanks