Hello,
I am a bit of a beginner with javascript and I can't think of a way around the folowing problem. I am using Mootools in this example, though this is not a Mootools question:
for (var i = 0; i < 5; i++) {
myElement[i].addEvent('click', function () { otherFunction(i); });
}
Whenever someone clicks on myElement, otherFunction is called (good) but is passed 5 (bad). I know this is because it acesses i AFTER the loop finishes (when they click on the element), but I can't for the life of think of any alternative except for the monotonous
switch(i) {
case 1: myElement[i].addEvent('click', function () { otherFunction(1); }); break;
case 2: myElement[i].addEvent('click', function () { otherFunction(2); }); break;
// ...
}
There's got to be a better way... I feel I'm missing something obvious
UPDATE: Added [i] index to myElement (oops)
Thanks, Cameron