View Full Version : Add a sequential number to each list item?

05-26-2010, 12:35 AM
I am wanting to add a sequential number starting at either 0 or 1 next to each list item via javascript, so if I have;


so the result is something like

<li>1 Item</li>
<li>2 Item</li>
<li>3 Item</li>
<li>4 Item</li>
<li>5 Item</li>

Note however it would need to be specific to this list set so that all other lists on the page are not affected, I assume a name tag in the UL should cover that?

Hope that makes sense and thanks guys

05-26-2010, 01:15 AM
Is there then a reason not to use <ol>, ordered list? It's like <ul> but instead of "unordered", it has numbers.

05-26-2010, 07:39 AM
Yes I have my reasons not to use OL, the number will be added to a class as well :)

05-26-2010, 09:33 AM
I have been trying to get my head around javascript increments, in particular I have been reading the info at http://www.webdevelopersnotes.com/tutorials/javascript/javascript_increment_decrement_operators.php3

namely from the example

var a = 5;
b = a++;
(b contains the initial value of a, which is 5.
a, on the other hand is now equal to 6)

var a = 5;
c = ++a;
(In this case, JavaScript first adds 1 to a, changing
its value to 6. This value is returned to c.
Thus, c = 6)

But how do I get it to increment, so from the above (first part) if I call 'a' I get 6, which is fine, but if a is called again it is still 6 and not 7... how do I get a to be 6, 7 , 8 and so on?

Likewise in the second part, where c is returned as 6, how do I get it to return as 7 on the next call on the same list?

I am trying to learn...

Here is a sample of what I am trying to get to work


where 'dgsx' is the same as 'a' in the above example.

But all I get is the class all having the same number at the end instead of it incrementing