7
var items = $(".myClass");

The code above returns a number of items when used to select all elements. How can I select a specific element, for example the second one? Neither items(2) nor items[2] works.

4 Answers 4

13

Try this:

items.eq(2) // gets the third element as a jQuery object (zero-based index)

Source: http://docs.jquery.com/Traversing/eq#index

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Because I always use items.get(2) and I didn't know about this method. Does anyone know the difference?
@Andy: get(#) and [#] are the same and return the DOM element while eq(#) returns the jQuery object which wraps the DOM element.
0

arrays are zero based so you need items[1] for the second one

Comments

0

2nd item would be items[ 1 ] in your case. Also the code you've provided works perfectly for me (with items[ 1 ]).

Comments

0

try

var items = $(".myClass"); alert($(items)[1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.