jQuery .each() function. Resetting the index?
Posted
by Michael
on Stack Overflow
See other posts from Stack Overflow
or by Michael
Published on 2010-04-11T23:14:12Z
Indexed on
2010/04/11
23:23 UTC
Read the original article
Hit count: 296
jQuery
Hi there,
I have multiple lists with the same class.
I'd like to loop each list and its LI's and prepend the current number before each. The current code I have is:
$jQuery(".numberList li").each(function(i) {
var index = i + 1;
$jQuery(this).prepend("<span>" + index + "</span>");
});
The problem is, is that the index doesn't restart back at 0 for each list it goes through, it just keeps going up. For example, the output I get now is:
First list
1. item
2. item
3. item
Second list
4. item
5. item
6. item
Second list should start at 1 again by having the index back at 0.
Could someone point out where I'm going wrong? I'm not a jQuery expert or anything...clearly :)
Many thanks, Michael.
© Stack Overflow or respective owner