jQuery Next Adjacent Selector (“prev + next”)

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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

Syntax

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

jQuery
$("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)
CallWhat it does
prevSelector for the preceding sibling
+Adjacent sibling combinator
nextSelector for the immediate next sibling

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

Parameters

ParameterDescription
Step 1Confirm both elements are siblings under the same parent.
Step 2Write $("prev + next") — for example $("label + input").
Step 3Chain a method: .val(), .addClass(), events, and so on.

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

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

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.

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.

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: Next Siblings Selector

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

Next Siblings Selector 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.

8 people found this page helpful