user1015214
user1015214

Reputation: 3081

how to refer to the nth element in a jquery object

I am using a jquery selector to select a bunch of divs. I want to be able to ask for the 8th element of the resulting collection. How can I do this?

Upvotes: 0

Views: 42

Answers (2)

blackpanther
blackpanther

Reputation: 11486

You could try the following: $(selector)[n]; or in your case: $(selector)[7] Or even the use of :eq() $("div:eq(7)") if you are selecting the eighth <div> element.

Upvotes: 0

crush
crush

Reputation: 17013

If you want the nth element you'd do the following:

$(selector)[n-1];

If you need it as a jQuery object, you can get it from the eq() method:

$(selector).eq(n-1);

For example, the 1st element, would be

$(selector)[0]

Thanks Jason P - again

Upvotes: 3

Related Questions