I am trying to add an event handler to buttons which are generated in a browser extension. There are many buttons so it is done inside a loop, and the event handler of the button needs an argument (otherwise, all buttons will do the same thing).
for (....)
{
....
var arg = createArgument(...);
....
var myButton = document.createElement('button');
myButton.addEventListner('click', function(){myButtonClick(arg)});
....
}
function myButtonClick(arg)
{
I used a method in this answer to pass an argument to the function, but the problem is that the arg
for all buttons is set to the last value of arg
at the last iteration of the for
loop. A comment in that answer says that I have to use bind()
, but I could understand how. In my case, how should I use bind()
so that each button's event listener would have a different arg
?