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


Instead of writing,
$(“#step li”).each(function (i) {
i = i+1;
$(this).addClass(“item” i);
});
couldn’t we just change the background directly and not give it the class like,
$(“#step li”).each(function (i) {
i++;
$(this).css(“background” , “url(step” + i + “.png) no-repeat;”);
});
This will remove the need for us to specify classes like .item1, item2 …
Is it possible to use this code to build a jquery photo gallery? Or is that a completely different deal?
very nice tutorial. is it possible that we could make the background create through a php snippet?
Trey Cruz, is it a trick question? or was it only me found it a self-answerable question?
Awesome solution. Although, what if Javascript is turned off?
cool codes!
It looks pretty clean. I didn’t know about this feature. Thanks for this post! Keep it up!
Great tutorial. Thanks ;)
@Adhip: it’s best to leave the styling in the CSS. this allows you to change your style/design where you normally would, rather than hunting down your styling in javascript code. but yes, you are technically correct.
Thanks and great post!
Just a little typo on the content: “.. find it annonying?”.
Keep it up!
Not sure if it’s been mentioned but Expression Engine has a built-in counter (and perhaps other CMS, that’s only my fav). So adding a sequential number to each item is easy. The gist of it would be
li class=”item{count}”
which would output
li class=”item1″
li class=”item2″
And if you don’t need all the extra markup on ever single li, you can use a conditional to target just one
if count == 5
li class=”item{count}”
I’m building my company site and I’m getting a lot of great ideas from you here. These are RSS feeds worth keeping! Great application of the jQuery UI. I recently started using jQuery – had been using only Mootools before, but now I’m seeing the benefits and weaknesses of both engines.
Trey: Building a gallery out of jQuery is a totally different, and a deeper depth of coding. You’d have to incorporate a way to feed your images whether it be XML or just by plain HTML code, and then turn it into a Javascript masterpiece. Easiest thing to do – look up “Photo Gallery jQuery” on Google and you’ll see a LOT of different galleries already programmed for you!
Thank you my friend …
Fantastic tutorial on these codes.
Cheers Eva
This is a great article. Website owners often forget that it’s indeed about exposure to the content and not the website. A very usefull list with ‘old’ and new ways to promote your content and even drive sales.
Thanks Thanks What’s the problem here? Google could bury the meager profit number from even the biggest media conglomerates.
these are awesome!
thanks for putting in the effort to get this list together
Thank =)=)=) you http://www.cennet.gen.tr
Brilliant idea, love it :)
Sweeet!