tags:

views:

41

answers:

3

Hi friends, I got this html code:

<form id = "xxx">
   <ul>
     <li class = "req" > </li>
     <li class = "req" > </li>
     <li class = "req" > </li>
     <li > </li>    ==================> HOW CAN I ADD A CLASS="req" to this li with jquery
     <li class = "req" > </li>
     ....
    <li class = "req" > </li>
  </ul>
</form>

So I rewrite the question: There is one li tag that has not ( class= "req"), how can i add that with jquery? Thank you

+3  A: 
$("#xxx li").addClass("req");

is sufficient.

If you want to explicitly add it to only the li without req, you can do:

$("#xxx li:not(.req)").addClass("req");

If you want to only add the class to the fourth element, you would do:

$("#xxx li:eq(3)").addClass("req");

The 3 is because it uses a zero-based index for the elements.

Magnar
oh yes wonderful ! it works, but sorry, don't move am comin with another question
Mamadou
There are an other li tag without class req, but i don't want to add to theme the class, just to this one ! can i play with position ? or something like that ??
Mamadou
if it's always the fourth li inside that ul, you can do: `$("#xxx li").eq(3).addClass("req");`. Start counting by 0, so the fourth is number 3.
Litso
Thank you guys. It works
Mamadou
A: 

jQuery('li').addClass('req');

Andrew Dunn
A: 

With not you can check if an item has a certain attr.

$("ul#yourid").not(".req").addClas("req");
Tim
Can i add a class just to this li? The solution you gave work but i forgot i could have an other li without class, i mean it's not unique and it has not an a id
Mamadou