jQuery Next Siblings Selector (“prev ~ siblings”)

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

The general sibling combinator prev ~ siblings selects all following siblings that match siblings, share the same parent as prev, and appear after it in the DOM. Wider reach than +. Available since jQuery 1.0 (standard CSS).

Syntax

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

jQuery
$("prev ~ siblings")

$("#prev ~ div")  → every div sibling after #prev
$("h2 ~ p")       → every p sibling after each h2

$("h2 + p")       → only the immediate next p
$("div p")        → p nested inside div (descendant)
CallWhat it does
prevSelector for the starting sibling
~General sibling combinator
siblingsFilter for following siblings to keep

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

Parameters

ParameterDescription
Step 1Confirm targets are siblings under the same parent (not nested children).
Step 2Write $("prev ~ siblings") — for example $("#prev ~ div").
Step 3Chain a method on the whole following set.

Return Value

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

jQuery Examples

Example 1 — Official Demo: #prev ~ div

Select every div sibling that follows #prev (jQuery API example).

jQuery
var tags = $("#prev ~ div").map(function () {
  return this.tagName;
}).get().join(", ");

console.log($("#prev ~ div").length);
console.log(tags);
Try It Yourself

How It Works

  1. Anchor on #prev. Matching starts after that element.
  2. Keep following divs. Each sibling div after #prev is kept.
  3. Skip non-divs. A following span does not match the filter.

Example 2 — All Paragraphs After a Heading: h2 ~ p

Mark every paragraph sibling that follows an h2.

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

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

How It Works

  1. Start at h2. Every later sibling p is a candidate.
  2. Prefix each match. All three paragraphs get [~].
  3. Compare with +. Adjacent picks only the first paragraph.

Example 3 — General ~ vs Adjacent +

Count how far each combinator reaches on the same list.

jQuery
// Markup: #prev, div, div, span, div
console.log("~:", $("#prev ~ div").length);  // 3
console.log("+:", $("#prev + div").length);  // 1
Try It Yourself

How It Works

  1. Tilde scans forward. Every later matching sibling is kept.
  2. Plus stops early. Only the element right after #prev can match.
  3. Filter still applies. Both require the sibling to match div.

Frequently Asked Questions

The general sibling combinator prev ~ siblings selects every element matching siblings that comes after a sibling matching prev, as long as both share the same parent. $("h2 ~ p") matches all paragraphs after each h2, not only the first one.
+ reaches only the immediately following sibling. ~ extends that reach to all following siblings that match. $("h2 + p") picks one lead paragraph; $("h2 ~ p") picks every following paragraph sibling.
A space matches nested descendants inside an ancestor. ~ matches siblings at the same level. $("div p") finds paragraphs inside divs; $("h2 ~ p") finds paragraphs that follow an h2 as siblings.
No. Only siblings that appear after prev in the DOM. Elements before prev, and nested children of siblings (nieces/nephews), are skipped.
$("prev ~ siblings") is a one-shot CSS selector. .nextAll("siblings") is a traversal method when you already hold a jQuery collection and want every following sibling that matches a filter.
Since jQuery 1.0. It is standard CSS; modern browsers support it via document.querySelectorAll("h2 ~ p").

Next: [name] Selector — Has Attribute

Select elements by tag name — a building block often combined with sibling combinators.

[name] Selector — Has Attribute 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