jQuery is a very powerful tool which helps us to incorporate a variety of DOM traversal methods to select elements in a document randomly or in sequential order. Most of the DOM traversal methods do not modify the elements else they filter them out upon the given conditions.
The eq() method is an inbuilt method in jQuery which is used to locate the selected elements directly and returns an element with specific index.
Parameters: Here the parameter “index” specifies the index of the element.
Can either be positive or a negative number.
- The index number always starts at 0, so the first number will have index 0 (not 1).
- Using a negative number as an index starts the index count from the end of the list.
Below code will select the specified elements.
Below code will select the specified elements with negative index.
jQuery : eq() vs get()
- .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.
This article is attributed to GeeksforGeeks.org