jQuery .next() Method

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

What You’ll Learn

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

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.

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.

📝 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

⚡ Quick Reference

GoalCode
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()

📋 .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

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.

jQuery
$( "li.third-item" ).next().css( "background-color", "red" );
Try It Yourself

How It Works

Item 3’s next sibling is item 4. .next() returns exactly that one element — item 5 is ignored because it is not the immediate neighbor.

Example 2 — Official Demo: Label the Next Element After Disabled Buttons

Update the text of the span immediately following each disabled button.

jQuery
$( "button[disabled]" ).next().text( "this button is disabled" );
Try It Yourself

How It Works

Each disabled button’s very next sibling is a span. .next() targets that inline label without searching the whole document.

Example 3 — Official Demo: Keep Only Next Siblings Matching .selected

Filter the immediate next sibling — include it only when it has class selected.

jQuery
$( "p" ).next( ".selected" ).css( "background", "yellow" );
Try It Yourself

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.

jQuery
$( "#next-demo li.third-item" ).next().css( "color", "red" );

$( "#nextall-demo li.third-item" ).nextAll().css( "color", "blue" );

$( "#siblings-demo li.third-item" ).siblings().css( "color", "green" );



// .next() → item 4 | .nextAll() → 4,5 | .siblings() → 1,2,4,5
Try It Yourself

How It Works

All three methods stay among siblings under the same ul, but each defines a different slice. Pick .next() when you need exactly one step forward.

Example 5 — Highlight the Row Below the Selected Table Row

Real-world pattern: after clicking a row, style the next tr sibling for preview or expansion hints.

jQuery
$( "tr.selected" ).next().addClass( "preview-row" );
Try It Yourself

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().

🚀 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.

📝 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.

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 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
.next() Universal

Bottom line: Safe in any jQuery project. Use .next() for one step forward; use .nextAll() when you need every following sibling.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about .next()

One step forward among siblings.

5
Core concepts
02

.prev()

Mirror

Compare
→→ 03

.nextAll()

All ahead

Compare
? 04

Selector

Immediate only

Filter
0 05

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.

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