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
Fundamentals
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.
Concept
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).
Foundation
📝 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)
Cheat Sheet
⚡ Quick Reference
Goal
Code
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)")
Compare
📋 .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
Hands-On
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.
list item 3 → red background
Items 1, 2, 4, 5 → unchanged
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.
Third div in document order → blue background class
Other divs → unchanged
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.
eq(2) → jQuery object (chainable)
get(2) → same DOM node, not wrapped
Third li → blue text, bold weight
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.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.
Wrap Up
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).
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .eq()
One element by index.
5
Core concepts
🖼01
.eq(n)
One pick
API
002
Zero-based
Start at 0
Index
−103
Negative
From end
Since 1.4
∅04
Empty set
Out of range
Safe
.get05
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.