views:

140

answers:

1

I have an ordered list, but I want to number is manually in a separate span tag than use the default list-style: decimal;

<ol>
 <li><span>1</span> item 1</li>
 <li><span>2</span> item 2</li>
</ol>

How can I achieve this using jQuery?

Many thanks!

+6  A: 
$(function() {
  $("ol > li").each(function(i, n) {
    $(this).prepend("<span>" + (i+1) + "</span> ");
  });
});

You'll need to disable the standard markers too:

ol { list-style-type: none; }

Adjust as required.

cletus
Meh. Beat me to it. :-) +1
Tomalak
wow, is there anything you *can't* do with jQuery? :)
Nimbuz
Heavier than air flying machines.
Charlie boy
Oh, just one problem, it starts from 0, how do I make it to start from 1? Thansk!
Nimbuz
Er, the example above starts at 1. That's why it's (i+1).
cletus