Get element from selector by specifying index using jQuery

var items = $(".myClass"); 

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

+7
jquery jquery-selectors indexing
source share
4 answers

Try the following:

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

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

+13
source share

arrays are based on zero value, so you need items[1] for the second

0
source share

In your case, the 2nd item will be items[ 1 ] . Also the code you provided works fine for me (with items[ 1 ] ).

0
source share

try

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

0
source share

All Articles