Have you ever had to manually code something that is sequential? Didn't you find it annonying? Well, here is a simple solution for you. This tutorial will show you how to use jQuery to add a sequent of CSS classes to create a graphical list. The second example will show you how to add a comment counter to a comment list using jQuery's prepend feature.
1a. Insert jQuery Code
First, download a copy of jQuery. In between the <head> tag, insert the jQuery code:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#step li").each(function (i) {
i = i+1;
$(this).addClass("item" i);
});
});
</script>
The jQuery will output the source code as:

1b. CSS Code
Style the <li> element with a background image accordingly (step1.png, step2.png, step3.png, and so on..).
#step .item1 {
background: url(step1.png) no-repeat;
}
#step .item2 {
background: url(step2.png) no-repeat;
}
#step .item3 {
background: url(step3.png) no-repeat;
}
2a. Add Sequential Content
You can also use this technique to add sequential content using jQuery's prepend feature. The following example shows how you can add a counter number to a comment list.
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#commentlist li").each(function (i) {
i = i+1;
$(this).prepend('<span class="commentnumber"> #' i '</span>');
});
});
</script>
The code will add the <span class="commentnumber"> tag (with # + 1) to each <li> tag.

2b. CSS
Style the <li> element with position:relative and use position:absolute to place the .commentnumber to the top right corner.
#commentlist li {
position: relative;
}
#commentlist .commentnumber {
position: absolute;
right: 0;
top: 8px;
}


how well does this scale? i am guessing, not well.
Nice, efficient little enhancement! Building on what @Marco said, you could even trim it down to one line if you wanted:
$(“#step li”).each(function (i) {
$(this).addClass(“item” + (++i));
});
Great article. I also found some great jQuery information at http://www.jquerybasics.com
a lot of people seem to being saying this is a way to be lazy. to me this seems like the best way to make user generated content (or cms content) to look as part of a well thought out uniformed designed site, when so often it does not. thanks for sharing Nick.
Muito bom cara, gostei.
Porém uso o PHP para isso, para mim é mais facil, mais a dica acima é valida
:D
The idea is awesome,I would try to do such next time.
–eddie
super article. i will try it 2day. thanks.
Wow! Thanks for this very helpful tutorial!
I wonder what the advantage is of this way of working
i can see only one…
if you send 10 comments over from the server to the user, you have
10 times <span class=”commentnumber”> #1</span>
witch if you have large amounts of comments is faster to generate the number on the userside.
but i was wondering is there any other advantage to this usage?
because if it were only 10 comments (like on lots of blogs)
or paged comments (10 per page)
then this does not give that mutch of a speed differance right?
so if anyone can shed some light here i’d appriciate it
Sander
I see, jQuery is useful :)
and here is a nice example for refreshing old html codes….
I like the concept – just not sure if it serves a purpose which there is already a simple solution for?
Isn’t this what ordered lists are for? For example 1 if you defined the ordered list with a class of ‘steps’ you could access it directly and put one background image in place instead of bandwidth consuming individual ones? Atleast, that is the case if as in your example the only thing that is changing is the text that could be identically styled in html or via sifr.
Just a caution with this approach, (over)doing this will make it difficult for other designers/developers working on the same design as they need to be looking at javascript files for css hooks which will not be immediately apparent…
谢谢提供这样好的东东!!
Thakns, is great tutorial
Nice idea, and a nice example of how we can implement the same thing many ways.
Hey Nick, how did you do the comment guidelines above the comment box? Something like that on one of my sites would be really useful. Can you email me if you have an extra moment?
This is really great! Thank you!!
Regarding comment #34:
You mean like this?
.commentbox {
border-top-width: medium;
border-top-style: solid;
border-top-color: #666666;
}
Nevermind, I see what you’re talking about…. I misunderstood. You mean like this: http://plugins.jquery.com/project/accordion
Great article. I have write about this in my drupal/ajax site (tomorrow online).