jQuery Next Adjacent Selector (“prev + next”)

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

What Is the Next Adjacent Selector?

The adjacent sibling combinator prev + next selects elements matching next that are immediately preceded by a sibling matching prev. Both sides must share the same parent. Available since jQuery 1.0 (standard CSS).

Remember
$("prev + next")

$("label + input")  → input right after a label
$("h2 + p")         → first p after each h2 only

$("h2 ~ p")         → all following sibling p (general)
$("div input")      → input nested inside div (descendant)
$("div > input")    → direct child input (child)
UsePrefer
Immediate next sibling$("prev + next")
All following siblings$("prev ~ next")
From a held collection$el.next("next")

How to Use prev + next

Name the previous sibling, add +, then name the element that must sit right after it.

  1. Confirm both elements are siblings under the same parent.
  2. Write $("prev + next") — for example $("label + input").
  3. Chain a method: .val(), .addClass(), events, and so on.
  4. If you need every following match, use ~ instead of +.
PieceRequired?Role
prevYesSelector for the preceding sibling
+YesAdjacent sibling combinator
nextYesSelector for the immediate next sibling

Pseudocode

Pseudocode
for each element E matching "next":
    P = E.previousElementSibling
    if P exists and P matches "prev":
        and P.parent === E.parent:
            keep E

return jQuery(kept elements)

Cheat Sheet

Keep these facts handy while writing adjacent sibling selectors.

TopicDetail
Syntax$("prev + next")
MeaningImmediate next sibling only
Parent ruleBoth sides must share the same parent
vs ~+ = one; ~ = all following
vs spaceSibling relationship, not nesting
vs .next()Selector vs traversal method
SincejQuery 1.0 (standard CSS)

Live Preview

Pick a combinator against a small sample DOM and see which nodes match.

Sample markup mirrors the official label + input pattern plus sibling contrasts.

Result
$("label + input")
matched: 2
tags: INPUT, INPUT

/* + = immediate next sibling */

jQuery Examples

Short, copy-ready snippets. Open a browser console with jQuery loaded, or use Try It Yourself for a live editor.

Example 1 — Official Demo: label + input

Select inputs that sit immediately after a label (jQuery API example).

jQuery
$("label + input").each(function () {
  this.value = "Labeled!";
});

console.log($("label + input").length);
Try It Yourself

How It Works

  1. Find candidates. jQuery looks at every input in the document.
  2. Check the previous sibling. Keep the input only if the element right before it is a label.
  3. Set the value. Each match gets "Labeled!".
  4. Count. With two label+input pairs, .length is 2.

Example 2 — Lead Paragraph: h2 + p

Match only the paragraph that sits directly after each heading.

jQuery
$("h2 + p").each(function () {
  this.textContent = "[lead] " + this.textContent;
});

console.log($("h2 + p").length);
console.log($("h2 ~ p").length);
Try It Yourself

How It Works

  1. Target the lead. h2 + p keeps one paragraph per heading — the next sibling.
  2. Prefix the text. Matched leads show [lead].
  3. Compare with ~. h2 ~ p also includes later paragraphs after the same heading.
  4. Rule of thumb. Need only the first following sibling? Use +.

Example 3 — Adjacent + vs General ~

See how many siblings each combinator selects on the same list.

jQuery
// Markup: h2, p, p, p (same parent)
console.log("+:", $("h2 + p").length);  // 1
console.log("~:", $("h2 ~ p").length);  // 3
Try It Yourself

How It Works

  1. Plus stops at one. Only the first p after h2 matches +.
  2. Tilde continues. Every later sibling p still matches ~.
  3. Same parent required. Both combinators need siblings under one parent.
  4. Pick by intent. Lead line → +. Rest of the section → ~.

Example 4 — Sibling + vs Descendant Space

Adjacent siblings are not the same as nested descendants.

jQuery
console.log("span + input:", $("span + input").length);
console.log("div input:", $("div input").length);

// + → input immediately after a span sibling
// space → any input nested inside a div
Try It Yourself

How It Works

  1. Sibling check. span + input needs the input right after the span.
  2. Nesting check. div input finds inputs at any depth inside a div.
  3. Nested input fails +. An input inside a wrapper is not a sibling of the outer span.
  4. Common mix-up. Use space for “inside”; use + for “right after.”

Example 5 — Selector + vs .next()

Build the same adjacent match from a held collection with .next().

jQuery
var $byPlus = $("label + input");
var $byNext = $("label").next("input");

console.log($byPlus.length === $byNext.length);
console.log($byPlus.length);
Try It Yourself

How It Works

  1. Selector path. $("label + input") queries the document in one string.
  2. Traversal path. Start from labels, then take each immediate next sibling if it is an input.
  3. Same set. Lengths match when every label is followed by an input.
  4. When to choose. Static query → +. Already holding labels → .next().

Edge Cases & Pitfalls

Check these before relying on + in real projects.

trap

Anything between breaks +

A whitespace-only text node is ignored by browsers for previousElementSibling, but an element (even an empty <span>) between label and input prevents a match.

parent

Must share the same parent

Official docs: both sides of + (and ~) must be siblings. Nested elements never match adjacent sibling selectors.

mix-up

+ ≠ space ≠ >

Space = descendant. > = child. + = next sibling. Mixing them up is a common beginner error.

scope

Forms and wrappers

Many layouts wrap the input. Prefer label[for] / #id links, or .next() from a known label, when markup is not flat siblings.

Browser Support

The adjacent sibling combinator is standard CSS and has worked in jQuery since 1.0. Modern browsers implement it in native querySelectorAll.

✓ CSS · jQuery 1.0+

jQuery "prev + next"

Supported in all modern browsers. Native: document.querySelectorAll("label + input"). Universal CSS adjacent sibling combinator.

100% Standard CSS selector
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
Adjacent sibling (+) Universal

Bottom line: Safe everywhere jQuery runs. Use + for the immediate next sibling; use ~ when you need every following match.

Performance Notes

ApproachCostBest for
$("prev + next")Native CSS when possibleOne-shot adjacent queries
$("prev").next("next")Small traversal stepStarting from an existing set
$("prev ~ next")Larger result setAll following siblings

Prefer the narrowest combinator that matches your intent. + is cheaper to reason about than ~ because it returns at most one sibling per previous match.

Key Takeaways

  • Role: prev + next selects the immediate next sibling only.
  • Parent rule: both sides must share the same parent.
  • Not ~: use general sibling when you need every following match.
  • Traversal: pair with .next() when you already hold elements.

One line: $("prev + next") means “the element right after prev” — nothing further, nothing nested.

Frequently Asked Questions

The adjacent sibling combinator prev + next selects elements matching next that are immediately preceded by a sibling matching prev. $("label + input") matches only inputs whose direct previous sibling is a label.
+ matches only the single element directly after prev. ~ matches every following sibling that matches next and shares the same parent. $("h2 + p") picks one lead paragraph; $("h2 ~ p") picks all following paragraphs.
A space matches nested descendants inside an ancestor. + matches siblings at the same level — both sides must share the same parent. $("div input") finds inputs inside div; $("label + input") finds inputs right after a label sibling.
Yes. Official jQuery docs note that for both + and ~, the elements on either side of the combinator must share the same parent.
$("prev + next") is a one-shot CSS selector from the document. .next("next") is a traversal method when you already hold a jQuery collection and want its immediate next sibling.
Since jQuery 1.0. It is standard CSS; modern browsers support it via document.querySelectorAll("label + input").

Next: General Sibling (~)

Learn how prev ~ siblings selects every following sibling match, not just the adjacent one.

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

8 people found this page helpful