jQuery .eq() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Index select

What You’ll Learn

The .eq() traversing method reduces a jQuery collection to one element at a specified zero-based index — including negative indexes from the end. This tutorial covers the official list-item demos, div selection pattern, out-of-range behavior, and compares .eq() with .get(), .first(), and the :eq() selector.

01

Syntax

.eq(n)

02

Zero-based

Index 0 = first

03

Negative

From end

04

One element

Or empty

05

vs .get()

jQuery vs DOM

06

Since 1.1

Core API

Introduction

A jQuery selector like $("li") often matches multiple elements. Sometimes you need just one — the third list item, the last tab, the middle slide in a carousel. The .eq() method picks a single element from the current collection by its position.

Available since jQuery 1.1.2 (negative indexes since 1.4), .eq(index) returns a new jQuery object containing only the element at that index. The index is zero-based and refers to the position within the jQuery object — not the element’s rank in the entire DOM document.

Understanding the .eq() Method

Given a jQuery object with zero or more matched elements, .eq(index) constructs a new jQuery object from the element at the specified position. Positive indexes count from the start; negative indexes count from the end (.eq(-1) is the last element).

If no element exists at the given index, jQuery returns an empty collection (.length === 0) — no error is thrown. This makes .eq() safe to chain even when the index might be out of range.

💡
Beginner Tip

Human counting starts at 1; jQuery indexes start at 0. The third list item is .eq(2), not .eq(3).

📝 Syntax

General form of .eq:

jQuery
.eq( index )

Parameters

  • index (required) — an integer indicating the 0-based position of the element. A negative integer counts backwards from the last element (since jQuery 1.4).

Return value

  • A new jQuery object containing the single element at the given index, or an empty set if the index is out of range.

Official jQuery API list example

jQuery
$( "li" ).eq( 2 ).css( "background-color", "red" );
// Index 2 → third list item (list item 3)

⚡ Quick Reference

GoalCode
First element$("li").eq(0) or .first()
Third element (human count)$("li").eq(2)
Last element$("li").eq(-1) or .last()
Second from last$("li").eq(-2)
Raw DOM element at index$("li").get(2)
Selector at query time$("li:eq(2)")

📋 .eq() vs .get() vs :eq() vs .first()

Four ways to target a single element — different return types and timing.

.eq(n)
jQuery obj

Method on existing collection; chainable

.get(n)
DOM node

Raw element or undefined; not chainable

:eq(n)
selector

Filter at initial query time

.first()
eq(0)

Shorthand for index zero only

Examples Gallery

Examples 1–4 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demos for index-based selection.

Example 1 — Official Demo: Select the Third List Item

Apply a red background to the element at index 2 — the third item in a five-item list.

jQuery
$( "li" ).eq( 2 ).css( "background-color", "red" );
Try It Yourself

How It Works

Indexes are zero-based: 0 = first, 1 = second, 2 = third. The index refers to position in the jQuery collection returned by $("li"), in document order.

Example 2 — Official Demo: Negative Index from the End

Select the second-to-last list item with .eq(-2) — item 4 in a five-item list.

jQuery
$( "li" ).eq( -2 ).css( "background-color", "red" );
Try It Yourself

How It Works

Negative indexes count from the end: -1 is last, -2 is second from last. Useful when you want the final items without calling .length first.

📈 Practical Patterns

Out-of-range safety, div selection, and comparison with .get().

Example 3 — Official Demo: Out-of-Range Index Returns Empty Set

Request index 5 from a five-item list — no element matches, so nothing is styled.

jQuery
$( "li" ).eq( 5 ).css( "background-color", "red" );
// 5 items → valid indexes 0–4; eq(5) → empty set, no error
Try It Yourself

How It Works

jQuery does not throw on invalid indexes — it returns an empty jQuery object. Always check .length when the index depends on dynamic data.

Example 4 — Official Demo: Third div Gets Blue Class

Find all divs in the body and add class blue to the one at index 2 — official demo pattern.

jQuery
$( "body" ).find( "div" ).eq( 2 ).addClass( "blue" );
Try It Yourself

How It Works

After .find("div") builds a collection, .eq(2) narrows to one div before .addClass(). The index is relative to the filtered set, not all divs matched by a top-level selector alone.

Example 5 — .eq() vs .get() on the Same Index

See return type differences — jQuery object vs raw DOM element.

jQuery
var $third = $( "li" ).eq( 2 );
var domThird = $( "li" ).get( 2 );

console.log( $third instanceof jQuery );     // true
console.log( $third[ 0 ] === domThird );     // true — same DOM node
console.log( domThird instanceof jQuery );   // false — plain Element

$third.css( "color", "blue" );               // chain jQuery methods
domThird.style.fontWeight = "bold";          // native DOM property
Try It Yourself

How It Works

Both reference the same DOM node. Choose .eq() when continuing a jQuery chain; choose .get() when you need a native element for non-jQuery APIs.

🚀 Common Use Cases

  • Carousel slides — show or style the slide at a specific index.
  • Tab panels — activate .eq(activeIndex) among tab headers or content panes.
  • Table rows — highlight the first or last data row with .eq(0) or .eq(-1).
  • Pagination — select the current page item from a list of page links.
  • After filtering — pick one result from a narrowed .filter() collection.
  • Random selection — combine with Math.floor(Math.random() * n) for random picks.

🧠 How .eq() Picks One Element

1

Start with collection

jQuery object holds N matched elements in order.

Input
2

Resolve index

Positive from start; negative from end (since 1.4).

Index
3

Bounds check

Valid index → one element; invalid → empty set.

Safe
4

Return single-element set

Chain .css(), .addClass(), or .on() on the result.

📝 Notes

  • Available since jQuery 1.1.2; negative indexes since jQuery 1.4.
  • Index is zero-based and relative to the current jQuery collection.
  • Out-of-range indexes return an empty jQuery object — no exception.
  • .first() equals .eq(0); .last() equals .eq(-1).
  • :eq(n) in a selector string is evaluated at query time; .eq(n) filters an existing set.
  • Does not modify the DOM — only narrows which elements the jQuery object refers to.

Browser Support

.eq() has been part of jQuery since 1.1.2+ (negative indexes since 1.4). It is pure collection indexing with no browser-specific behavior.

jQuery 1.1+

jQuery .eq()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: array index on NodeList after converting, or element.children[n].

100% With jQuery loaded
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.eq() Universal

Bottom line: Safe in any jQuery project. Use .eq() for chainable single-element selection; use .get() when you need the raw DOM node.

Conclusion

The jQuery .eq() method selects exactly one element from the current collection by zero-based index — with negative indexes counting from the end. It is the standard way to target a specific item in a matched set while keeping jQuery chaining available.

Remember the official list lesson: .eq(2) highlights the third item, not the second. When the index might be invalid, check .length on the result. For the raw DOM node instead of a jQuery wrapper, use .get(index).

💡 Best Practices

✅ Do

  • Use .eq(0) or .first() for the opening item
  • Use .eq(-1) or .last() for the final item
  • Check .length after .eq() when index is dynamic
  • Apply .eq() after .filter() to pick from results
  • Prefer .eq() over :eq() when filtering an existing set

❌ Don’t

  • Confuse human counting (1-based) with jQuery indexes (0-based)
  • Expect an error when the index is out of range — you get an empty set
  • Use .eq() when you need a raw DOM node — use .get()
  • Assume index matches DOM tree position globally — it is collection-relative
  • Chain .get(n).css().get() returns a DOM element, not jQuery

Key Takeaways

Knowledge Unlocked

Five things to remember about .eq()

One element by index.

5
Core concepts
0 02

Zero-based

Start at 0

Index
−1 03

Negative

From end

Since 1.4
04

Empty set

Out of range

Safe
.get 05

vs get

DOM node

Compare

❓ Frequently Asked Questions

.eq(index) reduces the current jQuery collection to a single element at the specified zero-based position. It returns a new jQuery object containing that one element — or an empty set if the index is out of range.
Yes. .eq(0) selects the first element, .eq(1) the second, and so on. The index refers to the position within the current jQuery object, not necessarily the element's position in the full DOM tree.
Since jQuery 1.4, a negative index counts from the end of the set. .eq(-1) is the last element, .eq(-2) is second from last. This is useful when you want the final item without knowing the collection length.
.eq() returns an empty jQuery object with length 0. Chaining methods on it is safe — they simply do nothing. No error is thrown.
.eq(index) returns a jQuery object wrapping one element (or empty), so you can keep chaining jQuery methods. .get(index) returns the raw DOM element (or undefined if out of range) — use it when you need native DOM APIs without jQuery wrapping.
Both pick by index, but :eq(n) is used inside a selector string at query time — $('li:eq(2)'). .eq(n) is a method applied after you already have a jQuery collection — $('li').eq(2). Prefer .eq() when filtering an existing set; use :eq() when building the initial selector.
Did you know?

The jQuery API explicitly notes that .eq() index refers to position within the jQuery object, not position in the DOM tree. After $("ul").find("li"), .eq(0) is the first li found inside any matched ul — not necessarily the first li on the entire page. Context from earlier chain steps defines what you are indexing.

Back to jQuery Traversing

Explore more DOM traversal methods as the collection grows.

Traversing hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful