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 :-)
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>
$("#list li").each(function(i) {
this.addClass("item"+(i+1));
});
Here it is in action
http://jsbin.com/ocake
Update per comments, as in example link this works :
$(document).ready(function() {
$("#list li").each(function(i) {
$(this).addClass("item" + (i+1));
});
});
But I think initial code should work by adding :
this = $(this);
But not sure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With