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;
}


nice, thx for this article
thx, a very useful article!
Such a simple solution – Thanks!
Just one thing in your JS code:
“i = i+1;”
Change it to “i++” to make it shorter :) .
Thanks that’s very useful
Nice, but this really shouldn’t be done using JavaScript.
As ~James noticed – depending design on JS IMHO is not good idea. Why don’t you use CSS3 selectors (nth-child)?
Of course, older browsers won’t understand it, but logic are still semantic. I think, the best thing nowadays is generating adequate classes on the server-side. Alternatively, you may use IE’s expression and CSS3 selectors. ;)
The simple ‘ul’ and ‘li’ codes can make a lot of arts.
If you use wordpress or other CMS’s you can use those, so why using JS on a site? If it’s turned off, you’ll end up with nothing?
I prefer using JS for motion and such… This way the site still remains functional, is JS is turned off.
nice,wonderfull
Very usefull.
Nice effect.
It’s a nice simple solution. Personally, I don’t see that there’s a problem using javascript if the end result is an enhancement to the user interface (progressive enhancement). If someone has javascript turned off the page will still be usable (although it won’t be as pretty).
Thanks for sharing.
wicked tutorial, thanks
There are much better uses for JQuery then just accomodating laziness. Though surely the effort to do this if you aren’t already using JQuery on the site is about the same as adding in those classes.
I think the javascript code would be better off at the end of the document as well.
Nice effect thanks..
Cool tutorial, but as what it seems a lot of other people are saying: why use js? Regardless, it’s still a great tutorial!
- Greg
Very cool, and pretty easy too. Thanks!
thanks… Very useful….
Now I can be super lazy!!
Nice Tutorial Nick. Thanks for sharing !!
Thanks for the article. Jquery is truly wonderful!