I have problem with adding class to every list element. This is example html:
<ul id="main_list">
<li>item1</li>
<li>item2<li>
<ul>
And my jquery code which i don’t understand why it don’t work like i expect:
$(function(){
$('#main_list').children().each(function(){
$(this).addClass(function(){
return $(this).index()})
})
})
It’s because the jQuery object on which you’re calling
addClasshas only one item in it (you’re creating that jQuery object via$(this)). Try this:…if you’re really trying to assign classes like
0,1, etc. I think I’d probably put something in front of those numbers, though; I can’t immediately find it in the spec, but I don’t think class names can start with digits. So:…for
n0,n1, etc.