RobertD RobertD - 2 years ago 62
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>

Answer Source

Try this :

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

Reference :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download