3

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 Answers 3

4
$("#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.

Sign up to request clarification or add additional context in comments.

3 Comments

oh yes wonderful ! it works, but sorry, don't move am comin with another question
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 ??
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.
1

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

$("ul#yourid").not(".req").addClas("req");

1 Comment

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
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.