The .next() traversing method moves sideways in the DOM — it returns the immediately following sibling of each matched element, optionally filtered by a selector. This tutorial covers the official list-item and disabled-button demos, selector filtering, comparisons with .prev(), .nextAll(), and .siblings(), and practical row-navigation patterns.
01
One step
Next neighbor only
02
Selector
.next(".x")
03
vs .prev()
Forward / back
04
vs .nextAll()
One vs many
05
Empty safe
No neighbor OK
06
Since 1.0
Core API
Fundamentals
Introduction
Siblings share the same parent — think of list items in a ul, table rows in a tbody, or buttons in a toolbar. When you need the element right after your current selection, reach for .next().
Available since jQuery 1.0 (selector filtering since 1.4), .next([selector]) constructs a new jQuery object from the immediately following sibling of each element in the current set. It does not descend into children or climb to ancestors — it stays on the same level of the tree, one step forward.
Concept
Understanding the .next() Method
Given a jQuery object representing a set of DOM elements, .next() looks at each element’s nextElementSibling in the DOM. If that neighbor exists, it is added to the result. When you pass a selector, jQuery includes the neighbor only if it matches — otherwise the result for that element is empty.
This is narrower than .nextAll(), which collects every following sibling, and narrower than .siblings(), which can return both directions. .next() is the precision tool for “the very next one” — perfect for step wizards, inline help text beside a disabled control, or highlighting the row below a selection.
💡
Beginner Tip
$("li.third-item").next() returns list item 4 only. $("li.third-item").nextAll() returns items 4 and 5. One step vs everything ahead.
Foundation
📝 Syntax
General form of .next:
jQuery
.next( [ selector ] )
Parameters
selector (optional) — a string containing a selector expression. The immediately following sibling is included only if it matches this selector.
Return value
A new jQuery object containing the immediately following sibling of each element in the current set (filtered when a selector is provided).
Official jQuery API list example
jQuery
$( "li.third-item" ).next().css( "background-color", "red" );
// Red background on list item 4 — the very next sibling after item 3
Cheat Sheet
⚡ Quick Reference
Goal
Code
Immediate next sibling
$("li.active").next()
Next sibling only if it matches
$("p").next(".highlight")
Text beside disabled button
$("button[disabled]").next().text("…")
All following siblings
$("li.start").nextAll()
Previous sibling
$("li").prev()
Both directions
$("li").siblings()
Compare
📋 .next() vs .prev() vs .nextAll() vs .siblings()
Four sibling methods — same parent level, different scope and direction.
.next()
+1 forward
One immediately following sibling
.prev()
-1 backward
One immediately preceding sibling
.nextAll()
all ahead
Every following sibling after current
.siblings()
both ways
All siblings except self — optional filter
Hands-On
Examples Gallery
Examples 1–3 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 immediate next-sibling traversal.
Example 1 — Official Demo: Highlight the Next List Item
From the third list item, style the immediately following sibling — the core official lesson.
Paragraph whose immediate next sibling has .selected → yellow on that sibling
Other paragraphs → no match (next sibling lacks .selected)
How It Works
jQuery checks only the direct next sibling. If it lacks .selected, that paragraph contributes nothing to the result — jQuery does not skip ahead to a later matching sibling.
📈 Practical Patterns
Compare sibling methods, then navigate table rows with .next().
Example 4 — .next() vs .nextAll() vs .siblings()
See how scope changes when you move one step vs many.
Row immediately below .selected → .preview-row class
Last row (no next tr) → empty .next(), no change
How It Works
Table rows in a tbody are siblings. .next() moves to the single row below — ideal for master/detail UI without selecting every remaining row via .nextAll().
Applications
🚀 Common Use Cases
Inline help beside controls — $("input:invalid").next(".error-msg").show().
Step wizards — $(".step.active").next(".step").addClass("active") (when steps are adjacent siblings).
Accordion panels — toggle the panel immediately after a clicked header.
Table row actions — highlight or expand the row below the current selection.
Disabled control labels — official pattern: update text on the next span after button[disabled].
Chained navigation — .next().next() skips two siblings when layout guarantees adjacency.
🧠 How .next() Finds the Immediate Sibling
1
Start with source elements
jQuery object holds one or more DOM nodes.
Input
2
Read next sibling
For each node, get nextElementSibling.
Traverse
3
Apply selector filter
If provided, keep neighbor only when it matches.
Filter
4
→
Return & chain
New jQuery object — empty when no matching neighbor exists.
Important
📝 Notes
Available since jQuery 1.0 — optional selector argument since 1.4.
Returns at most one sibling per source element — not all following siblings.
Text nodes and comments between elements are skipped — jQuery uses element siblings only.
With a selector, only the immediate next sibling is tested — no further scanning.
Empty result returns an empty jQuery object — safe to chain without errors.
Pair with .prev() for backward navigation and .nextAll() when you need the full tail of siblings.
Compatibility
Browser Support
.next() has been part of jQuery since 1.0+ (selector form since 1.4). It relies on standard sibling traversal with no browser-specific behavior beyond jQuery itself.
✓ jQuery 1.0+
jQuery .next()
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: element.nextElementSibling — jQuery adds selector filtering and collection semantics.
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
.next()Universal
Bottom line: Safe in any jQuery project. Use .next() for one step forward; use .nextAll() when you need every following sibling.
Wrap Up
Conclusion
The jQuery .next() method returns the immediately following sibling of each element in the current collection. It is the simplest way to move one step forward among elements that share the same parent.
Remember the official list lesson: from item 3, only item 4 gets the red background. Use .nextAll() when you need everything ahead, .prev() to step backward, and .next(selector) when the very next neighbor must match before you act on it.
Use .next() when layout guarantees an adjacent sibling
Check .next().length before acting when the neighbor may not exist
Pair with semantic HTML — labels beside inputs, rows in tables
Use .next(selector) to require a specific neighbor type
Prefer .nextAll(selector).first() when matches may be further along
❌ Don’t
Use .next() when you need all following siblings — use .nextAll()
Expect .next(".x") to skip non-matching neighbors
Confuse siblings with children — use .children() or .find() for descendants
Assume text nodes count — only element siblings are returned
Chain .next().next() unless the DOM structure is stable and documented
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .next()
One step forward among siblings.
5
Core concepts
→01
.next()
+1 sibling
API
←02
.prev()
Mirror
Compare
→→03
.nextAll()
All ahead
Compare
?04
Selector
Immediate only
Filter
005
Empty
Safe chain
Edge case
❓ Frequently Asked Questions
.next() returns the immediately following sibling of each element in the current jQuery collection. It moves sideways along the DOM tree — not down into children and not up to parents. Without a selector, it always includes the very next sibling element if one exists.
.next() returns at most one sibling — the direct next neighbor. .nextAll() returns every following sibling after that point. From list item 3, .next() gives item 4 only; .nextAll() gives items 4 and 5.
.next() looks forward to the following sibling; .prev() looks backward to the preceding sibling. They are mirror-image methods — same distance (one step), opposite direction.
jQuery returns an empty jQuery object — length 0. Chaining still works safely: .next().addClass('x') simply does nothing when no neighbor exists. Test with .next().length before acting if needed.
No. .next(selector) checks only the immediately following sibling. If that sibling does not match the selector, the result is empty — jQuery does not keep scanning further siblings. Use .nextAll(selector).first() or .siblings(selector).first() when you need the next match further along.
Yes. jQuery runs .next() for each element in the current set and unions the results. $('p').next() returns the next sibling of every paragraph in the collection — duplicates are removed automatically in the jQuery object.
Did you know?
Before jQuery 1.4, .next() had no selector argument — developers wrote .next().filter(".selected") to test the neighbor. The optional selector parameter folded that two-step pattern into one call, matching how .nextAll(selector) already worked for multiple siblings.