jQuery .next() Method — Immediate Next Sibling

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("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
CallWhat it does
Current setElements whose next sibling you want
selectorKeep the next sibling only if it matches
Return valueNew jQuery object (0 or more next siblings)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the starting element(s).
Step 2Call .next() for the immediate following sibling.
Step 3Optionally pass a selector to keep that sibling only if it matches.

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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