Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element from selector given an index with jQuery

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.

like image 604
Fuxi Avatar asked Nov 25 '09 14:11

Fuxi


People also ask

What is the difference between EQ () and get () methods in jQuery?

eq() returns it as a jQuery object, meaning the DOM element is wrapped in the jQuery wrapper, which means that it accepts jQuery functions. . get() returns an array of raw DOM elements. You may manipulate each of them by accessing its attributes and invoking its functions as you would on a raw DOM element.

Which method is used to get the element which has a specific index number of the selected elements?

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Can you use CSS selectors in jQuery for selecting elements?

Projects In JavaScript & JQueryjQuery uses CSS selector to select elements using CSS. Let us see an example to return a style property on the first matched element. The css( name ) method returns a style property on the first matched element. name − The name of the property to access.

What is slice () method in jQuery?

slice() method constructs a new jQuery object containing a subset of the elements specified by the start and, optionally, end argument. The supplied start index identifies the position of one of the elements in the set; if end is omitted, all elements after this one will be included in the result.


1 Answers

Try this:

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

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

like image 153
mbillard Avatar answered Sep 21 '22 22:09

mbillard