UserIsCorrupt
UserIsCorrupt

Reputation: 5025

Add class to an element based on placement with JavaScript/jQuery

<ul>
 <li>First</li>
 <li>Second</li>
 <li>Third</li>
</ul>

Is it possible to add a class to the second li listed above with jQuery, without a prexisting class or id?

Upvotes: 2

Views: 77

Answers (1)

jAndy
jAndy

Reputation: 236032

Of course, you would go like

$('ul li:eq(1)').addClass('yourclassname');

Since the selector is zero-based, 1 represents the second node.

Reference: http://api.jquery.com/eq-selector/

Upvotes: 3

Related Questions