Hello there,
Let me paint you a picture:
for (j = 0; j < btnArr.length; j++)
{
var btn = document.createElement("button");
btn.addEventListener("click", function() { press(this, j) }, false);
div.appendChild(btn);
}
The issue at hand is that by the time the event fires, the value of j
has already changed. I desire for the function to utilize the exact value of j
when it is defined, not merely reference it.
If only the 2nd parameter of addEventListener
could be a string, it might resemble this:
btn.addEventListener("click", "function() { press(this, " + j + ") }", false);
Any idea if such a thing is possible and how one might go about accomplishing it?
I've conducted some research but found no relevant information, as expressing the problem succinctly has proven quite challenging.
Just so you know, this pertains to a Greasemonkey script, hence the utilization of .addEventListener()
instead of .onclick = (...)