0

I would like to pass constant arguments to onclick listener. I know it's possible in HTML to do this :

<element onclick="myScript(453);"> 

But in JS I can't do this :

for (var i = 0; i < data.length; i++) {
    button = document.createElement('button');
    button.onclick = function () {
        myScript(i);
    };
}

This behavior is because i becomes undefined at end of loop or at least it is equal to data.length.
What I would like to do is instead of passing i, passing only the current value of it.

Gwendal
  • 67
  • 7

0 Answers0