I have 10 divs, with id's key0, key1, key2, etc. In my initialize function I was trying to run a loop to add event handlers to all of them, like so:
for (x=0; x<=9; x++){
document.getElementById("key" + x).addEventListener("click", function(){ numClick(x) }, false);
}
The goal being to set up each div with an event that would send the corresponding number to the numClick function when it was clicked. I mistakenly expected it to set the value of the variable when the event handler was assigned, but of course it doesn't. So every time one of the divs is clicked it always sends a value of 10, since that is what x ends up at after the loop.
I've been trying to figure out a way to make this work but so far I haven't come up with anything.