views:

115

answers:

6

Hello,

is it possible, to add auto numeric classes to a list by using jquery?

html:

<ul id="list">
 <li>Element 1</li>
 <li>Element 2</li>
 <li>Element 3</li>
 <li>Element 4</li>
 <li>Element 5</li>
</ul>

i want to get something like this:

<ul id="list">
 <li class="1">Element 1</li>
 <li class="2">Element 2</li>
 <li class="3">Element 3</li>
 <li class="4">Element 4</li>
 <li class="5">Element 5</li>
</ul>

hope there is a solution available :-)


Edit

ok, mhhm but my list has not always a number at the end. so what's about a classname combination, like "item + number" ? is something like this possible?

<ul id="list">
 <li class="item1">Element x</li>
 <li class="item2">Element c</li>
 <li class="item3">Element a</li>
 <li class="item4">Element d</li>
 <li class="item5">Element f</li>
</ul>
A: 
$('ul#list li').each(function(){
  $(this).addClass( $(this).text().split(' ')[1] );
});
clyfe
+1  A: 
$("#list").children().each(function(i) {
  $(this).addClass("prefix_" + (i+1));
});
Jeriko
+1, simplest solution and doesn't require string parsing. Small fix: should be `this.addClass(i+1)`.
interjay
This is assuming you want to add the class based on the elements position, as opposed to the text contained inside it (which everyone else seems to have assumed..)
Jeriko
Ah yes, i+1, thanks :D
Jeriko
thanks :-), but that function does not work, also the i+1 fix doesn't :-??
Svensson
corrected it, should work now!
Jeriko
no, it does not :?? but firebug reports no problems.. strange
Svensson
tested the above code on my pc - it definitely works - make sure your <ul> has the id of "list"
Jeriko
This is overkill. For this specific purpose, `$("#list li")` is far better than `$("#list").children()`.
macek
Care to explain why it's far better?
Jeriko
+1  A: 

CSS 2 has some special rules relating to numeric class names. See the grammar, specifically "class" within G.1, "nmstart" in G.2, and the final bullet point in G.3.

Using classes .c1 through .c5:

$('#list li').each(function(){
    $(this).addClass( 'c' + $(this).text().substr(-1) );
});

Note that this assumes the very last character of the <li> is a number. You may have to tweak (possibly using regex) for your exact use case.

Adam Backstrom
I think numbers are allowed - see the last line in your link.
interjay
Ah, thanks for pointing it out. This was a case where years ago I hit a browser that didn't support numeric classes, so I stopped using them, then this post prompted me to find out the "why" :)
Adam Backstrom
@Adam Backstrom, this solution only works for single-digits/characters
macek
@macek I know, I already pointed that out in my answer. Ostensibly this is a site for programmers, who I hope would have some ability to adapt what they are learning within this site.
Adam Backstrom
A: 

ok, mhhm but my list has not always a number at the end. so what's about a classname combination, like "item + number" ? is something like this possible?

<ul id="list">
 <li class="item1">Element x</li>
 <li class="item2">Element c</li>
 <li class="item3">Element a</li>
 <li class="item4">Element d</li>
 <li class="item5">Element f</li>
</ul>
Svensson
If your question has changed, you should delete this answer and instead rewrite the question.
Adam Backstrom
@Adam Backstrom, I cleaned this up for him.
macek
+3  A: 
   $("#list li").each(function(i) {
     this.addClass("item"+(i+1));
    });

Here it is in action

http://jsbin.com/ocake

c0mrade
A: 

For jQuery 1.4.x:

$("#list > li").addClass(function(i){return "item" + (i + 1);});
Kobi