For example:
var items = jq(".li");
for(var i=0; i<items.length; i++){
var item = jq(items[i]);
item.click(function(i){
console.log("i: ", i);
});
}
The problem with this code is that when the click event is fired, i returns undefined. I imagine that this happens because it is at that moment when it goes to look for the value of i which no longer exists since the loop has finished.
How can i fix this? I can't pass a parameter to an anonymous function.
Thanks in advance.
This possibility occurs to me, basically add an attribute to each element that will store the value of
i
:Hope this can help you :)
Another possible option that occurred to me: