bind click-event via loop
Hello,
I would like to substitute the following lines with a loop, such that I do not have to copy them countless times... (array stores jQuery-objects / with the $ in front)
- array[0].click(function() { someFunction(0); });
- array[1].click(function() { someFunction(1); });
- array[2].click(function() { someFunction(2); });
- ...
I tried:
- var imax = ...;
- for (var i = 0; i <= imax; i++) {
- array[i].click(function(){
- someFunction(i);
- });
- }
and:
- for (var i = 0; i <= imax; i++) {
- array[i].bind('click', function(){
- someFunction(i);
- });
- }
Both do not work.
Can someone please explain the reason for this (I'm guessing the assignment only 'lives' during execution of the loop-cycle) and maybe suggest a way around it?
Many Thanks.