Three Methods to Get Elements by Index in jQuery and Their Differences

Dec 03, 2025 · Programming · 5 views · 7.8

Keywords: jQuery | DOM manipulation | index retrieval

Abstract: This article provides an in-depth exploration of three primary methods for retrieving DOM elements by index in jQuery: array index access, the .get() method, and the .eq() method. Through comparative analysis, it explains the differences in return types and their impact on subsequent operations, emphasizing the critical distinction between DOM elements and jQuery objects when calling methods like .css(). With practical code examples, the article demonstrates how to correctly use the .eq() method to modify element background colors, avoid common pitfalls, and offers performance optimization tips and best practices.

In web development, the jQuery library is widely favored for its concise syntax and powerful DOM manipulation capabilities. When developers need to retrieve a specific element from a matched set based on its index, they often encounter multiple options. This article systematically analyzes three commonly used index-based retrieval methods: array index access, the .get() method, and the .eq() method, exploring their distinctions and best practices in practical applications.

Comparison of Three Index-Based Retrieval Methods

jQuery offers various ways to get elements by index, but each method returns a different data type, which directly affects subsequent operations. Understanding these differences is crucial for writing correct and efficient code.

Array Index Access

A jQuery object is essentially an array-like object containing matched DOM elements. Thus, you can directly use array index syntax to access an element at a specific position:

var element = $('ul li')[index];

This method returns a native DOM element (an Element object), not a jQuery object. This means you cannot directly call jQuery methods like .css() or .addClass(). Attempting to do so will result in a JavaScript error, as DOM elements lack these methods.

The .get() Method

The .get(index) method is similar to array index access, also returning the DOM element at the specified index:

var element = $('ul li').get(index);

According to the jQuery documentation, the .get() method "retrieve[s] the DOM elements matched by the jQuery object." Like array index access, it returns a native DOM element, so it cannot directly use jQuery methods. This may be useful in scenarios requiring pure DOM operations but is generally less convenient than returning a jQuery object.

The .eq() Method

The .eq(index) method differs fundamentally from the previous two: it returns a jQuery object containing the element at the specified index:

var $element = $('ul li').eq(index);

Per the documentation, the .eq() method "reduce[s] the set of matched elements to the one at the specified index." Since it returns a jQuery object, you can chain other jQuery methods like .css() or .addClass(), making code more concise and readable.

Practical Application Examples

Consider a common requirement: changing the background color of a list item in an unordered list based on its index. Below is a comparison of correct and incorrect approaches.

Incorrect Approach

Using array index or .get() to retrieve a DOM element and then attempting to call the .css() method directly:

// Incorrect: DOM elements do not have a .css() method
$('ul li')[index].css({'background-color': '#343434'});
$('ul li').get(index).css({'background-color': '#343434'});

Both lines will cause a JavaScript error because DOM element objects lack the .css() method. This is a common mistake among beginners, stemming from insufficient understanding of return types.

Correct Approach

Using the .eq() method to get a jQuery object and then calling the .css() method:

// Correct: jQuery objects have a .css() method
$('ul li').eq(index).css({'background-color': '#343434'});

Alternatively, if you have already obtained a DOM element via array index or .get(), you can wrap it back into a jQuery object:

// Convert DOM element to jQuery object
var domElement = $('ul li')[index];
$(domElement).css({'background-color': '#343434'});

However, this approach is less concise than directly using .eq() and may involve additional function call overhead.

Performance and Best Practices

From a performance perspective, the three methods have similar overhead when retrieving elements, but .eq() might slightly increase memory usage due to returning a jQuery object. In most application scenarios, this difference is negligible. More important considerations are code maintainability and readability.

Best practice recommendations:

  1. When subsequent jQuery operations are needed, prefer the .eq() method, as it supports method chaining for cleaner code.
  2. If only native DOM operations are required (e.g., directly accessing .innerHTML or .addEventListener), use array index or .get().
  3. Avoid mixing DOM elements with jQuery methods, as this can cause errors and reduce code readability.
  4. When manipulating multiple elements in a loop, consider using the .each() method or directly operating on the jQuery collection instead of retrieving elements individually.

Conclusion

Understanding the three methods for getting elements by index in jQuery and their differences is key to writing efficient, error-free code. The .eq() method is the preferred choice in most scenarios due to its return of a jQuery object, especially when method chaining is needed. Array index and .get() have their place when native DOM operations are required. Developers should choose the appropriate method based on specific needs and always be mindful of return types to avoid common invocation errors.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.