How to get a specific jQuery item from a list of items?

user798596 picture user798596 · Sep 22, 2011 · Viewed 22k times · Source

I have this:

<ul>
    <li>first</li>
    <li>second</li>
    <li>third</li>
    <li>fourth</li>
</ul>

Then I select it all with jQuery: $('ul').find('li'); or $('ul li');

How can I, from those two jQuery selectors get the, for instance only second li, or third, and to leave first and fourt alone?

I thought it might work with:

$('myselector').get(indexNumber); // however, it won't work.

Any ideas for this issue? Thanks.

Answer

Guffa picture Guffa · Sep 22, 2011

The get method returns the DOM element, so then you would have to wrap it inside a new jQuery object.

You can use the eq method:

var j = $('ul li').eq(1); // gets the second list item