jQuery .next() Method — Immediate Next Sibling

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is the .next() Method?

.next([selector]) (since jQuery 1.0) returns the immediately following sibling of each element in the set. With an optional selector, that sibling is kept only if it matches.

Remember
$("li.third-item").next();           // item 4
$("button[disabled]").next();        // next sibling after each disabled button
$("p").next(".selected");            // next sibling only if it has .selected

// Not the same:
$("li").nextAll();                   // all following siblings
NeedUse
One step forward.next()
One step backward.prev()
All following siblings.nextAll()
Following until a stop.nextUntil()
All siblings either side.siblings()

How to Use .next()

Start from an element, then take one step to the right in the sibling list.

  1. Select the starting element(s).
  2. Call .next() for the immediate following sibling.
  3. Optionally pass a selector to keep that sibling only if it matches.
  4. If no next sibling (or no match), the result is empty — chaining is still safe.
  5. Style or act on the returned set.
PieceRequired?Role
Current setYesElements whose next sibling you want
selectorNoKeep the next sibling only if it matches
Return value—New jQuery object (0 or more next siblings)

Pseudocode

Logic
for each element E in current set
  N = immediate next element sibling of E
  if N does not exist
    skip
  else if selector provided and N does not match selector
    skip
  else
    keep N

Cheat Sheet

GoalCode
Next sibling$("li.start").next()
Next only if class matches$("p").next(".selected")
Label after disabled buttons$("button[disabled]").next()
Previous sibling$("li.start").prev()
All after this one$("li.start").nextAll()
Check if a next existsif ($el.next().length) { … }

Live Preview

Start at item 3 and compare .next(), .nextAll(), and .prev().

  • list item 1
  • list item 2
  • list item 3 (start)
  • list item 4
  • list item 5

Start is item 3. Item 4 has class mark.

Result
Ready — click Run to traverse.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

Example 1 — Official List Demo

From the jQuery docs: red background on the item right after li.third-item.

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

How It Works

  1. Start. Select li.third-item (item 3).
  2. next. Move one step to item 4.
  3. css. Paint only that neighbor.
  4. Scope. Item 5 is not included — that needs .nextAll().

Example 2 — Label After Disabled Buttons

Official pattern: change the text of the sibling right after each disabled button.

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

How It Works

  1. Find. Select every button[disabled].
  2. next. Take each button’s immediate sibling (often a span).
  3. text. Set a clear status message.
  4. Skip. Enabled buttons are not in the start set.

Example 3 — .next(".selected")

Keep the next sibling only when it has class selected.

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

How It Works

  1. Start. Every p is a starting point.
  2. next. Look at each paragraph’s immediate sibling.
  3. Filter. Keep it only if it matches .selected.
  4. No skip. A later .selected further down is ignored.

Example 4 — .next() vs .nextAll() vs .siblings()

One neighbor, all after, or every sibling — count the difference.

jQuery
var $start = $("li.third-item");
console.log(".next() →", $start.next().length);
console.log(".nextAll() →", $start.nextAll().length);
console.log(".siblings() →", $start.siblings().length);
Try It Yourself

How It Works

  1. next. Exactly one step forward.
  2. nextAll. Every sibling after the start.
  3. siblings. All neighbors — before and after.
  4. Pick. Use the smallest method that fits the job.

Example 5 — Highlight the Next Table Row

From a selected row, style the row immediately below it.

jQuery
$("tr.selected").next().css("backgroundColor", "#fef9c3");
Try It Yourself

How It Works

  1. Start. Find the selected tr.
  2. next. Move to the following row sibling.
  3. Style. Highlight that neighbor row.
  4. Empty. Last-row start → no next → nothing happens.

Edge Cases & Pitfalls

CaseWhat happensFix
No next siblingEmpty setCheck .next().length if needed
Expecting skip to next match.next(".x") may be emptyUse .nextAll(".x").first()
Text / comment nodesIgnored — element siblings onlyNormal for jQuery traversal
Wanting all after.next() returns oneUse .nextAll()
Going backwardWrong directionUse .prev()

Browser Support

.next() has been part of jQuery since 1.0. It works in every browser where jQuery runs — a core traversing method with no deprecation.

✓ jQuery 1.0+

jQuery .next()

Stable across jQuery 1.x–3.x. Optional selector filter has been available since the method was introduced.

Stable Status
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() Since 1.0

Bottom line: Safe for production and learning. Pair with .prev(), .nextAll(), and .siblings() for full sibling navigation.

Performance Notes

ApproachCostBest for
.next()CheapestOne-step sibling moves
.nextAll()More workCollecting every following sibling
.siblings()More workBoth directions at once

Prefer .next() when you only need the immediate neighbor. Wider methods walk more of the sibling list.

Key Takeaways

  • Role: one step to the immediately following sibling — since 1.0.
  • Filter: .next(sel) keeps that sibling only if it matches — no skipping.
  • vs .nextAll(): one neighbor vs all following.
  • Mirror: .prev() does the same step backward.

One line: .next() returns the immediate next sibling — use .nextAll() when you need everything after.

Frequently Asked Questions

.next() returns the immediately following sibling of each element in the current set. It moves sideways in the DOM — not into children and not up to parents. Without a selector, the very next element sibling is included if one exists. Available since jQuery 1.0.
.next() returns at most one sibling — the direct next neighbor. .nextAll() returns every following sibling. 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. Same distance (one step), opposite direction.
jQuery returns an empty collection (length 0). Chaining stays safe: .next().addClass("x") does nothing when no neighbor exists. Check .next().length when you need a boolean.
No. .next(selector) tests only the immediately following sibling. If that sibling does not match, the result is empty — jQuery does not keep scanning. Use .nextAll(selector).first() when you need the next match further along.
Yes. jQuery runs .next() for each element in the set and combines the results. $("p").next() returns the next sibling of every paragraph.

Next: .nextAll() Method

Collect every following sibling — not just the immediate one.

.nextAll() tutorial →

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.

10 people found this page helpful