Add class to html tag using jquery
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: is there an li tag that does not have (class = "req"), how can I add this using jquery? Thanks you
+3
3 answers
$("#xxx li").addClass("req");
.
If you want to explicitly add it only to liwithout req, you can do:
$("#xxx li:not(.req)").addClass("req");
If you want to add a class only to the fourth element, follow these steps:
$("#xxx li:eq(3)").addClass("req");
3lies in the fact that it uses a zero index for elements.
+4