RobertD RobertD - 7 months ago 5
Javascript Question

How to add class to the third <li> element in javascript

I want to add class for the third element in javascript. I have this structure:

<ul class="products-grid row four-columns first">
<li class="item"></li>
<li class="item"></li>
<li class="item add-class-here"></li>
<li class="item"></li>
</ul>

Answer

Try this :

$( "ul.products-grid li:nth-child(3)").addClass("new_css_class");

Reference : https://api.jquery.com/nth-child-selector/