jQuery Next Siblings Selector (“prev ~ siblings”)

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

What Is the Next Siblings Selector?

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

Remember
$("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)
UsePrefer
All following siblings$("prev ~ siblings")
Immediate next only$("prev + next")
From a held collection$el.nextAll("filter")

How to Use prev ~ siblings

Name the starting sibling, add ~, then filter which following siblings to keep.

  1. Confirm targets are siblings under the same parent (not nested children).
  2. Write $("prev ~ siblings") — for example $("#prev ~ div").
  3. Chain a method on the whole following set.
  4. If you need only the immediate neighbor, switch to +.
PieceRequired?Role
prevYesSelector for the starting sibling
~YesGeneral sibling combinator
siblingsYesFilter for following siblings to keep

Pseudocode

Pseudocode
for each element P matching "prev":
    for each following sibling S of P:
        if S matches "siblings":
            keep S

// skipped: elements before P
// skipped: nested children of siblings (nieces)

return jQuery(kept elements)

Cheat Sheet

Keep these facts handy while writing general sibling selectors.

TopicDetail
Syntax$("prev ~ siblings")
MeaningAll following matching siblings
DirectionAfter prev only (not before)
Parent ruleSame parent required
vs +~ = many; + = one
vs spaceSiblings, not nested descendants
vs .nextAll()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 #prev ~ div pattern plus heading siblings.

Result
$("#prev ~ div")
matched: 3
tags: DIV, DIV, DIV

/* ~ = all following siblings */

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: #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.
  4. Skip nieces. A div nested inside a sibling is not a sibling of #prev.

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.
  4. Use case. Shared styling for a whole section body after a heading.

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.
  4. Pick by reach. One neighbor → +. Whole trail → ~.

Example 4 — Gaps Break +, Not ~

An element between prev and a later match still allows ~ to succeed.

jQuery
// Markup: h2, span, p, p
console.log("h2 + p:", $("h2 + p").length);  // 0 (span in between)
console.log("h2 ~ p:", $("h2 ~ p").length);  // 2
Try It Yourself

How It Works

  1. Adjacent needs contact. If the next sibling is a span, h2 + p fails.
  2. General looks further. Later p siblings still match h2 ~ p.
  3. Same parent still required. Nested paragraphs inside other tags do not count.
  4. Practical tip. Unstable markup between nodes? Prefer ~ (or traversal).

Example 5 — Selector ~ vs .nextAll()

Build the same following set from a held collection with .nextAll().

jQuery
var $byTilde = $("#prev ~ div");
var $byNextAll = $("#prev").nextAll("div");

console.log($byTilde.length === $byNextAll.length);
console.log($byTilde.length);
Try It Yourself

How It Works

  1. Selector path. $("#prev ~ div") queries the document in one string.
  2. Traversal path. Start at #prev, then take every following sibling that is a div.
  3. Same set. Lengths match for the same markup.
  4. When to choose. Static query → ~. Already holding the start node → .nextAll().

Edge Cases & Pitfalls

Check these before relying on ~ in real projects.

niece

Nested children are not siblings

Official demo: a “niece” div inside a sibling is skipped. ~ never walks into nested trees.

before

Nothing before prev matches

~ is forward-only. Elements that appear above prev in the markup are never selected.

mix-up

~ ≠ + ≠ space

+ = one neighbor. ~ = all following. Space = nested descendants. Pick the combinator that matches your tree shape.

scope

Wide matches on large pages

$("div ~ p") can be huge. Scope with an id/class on prev, or start from a container before querying.

Browser Support

The general 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 ~ siblings"

Supported in all modern browsers. Native: document.querySelectorAll("h2 ~ p"). Universal CSS general 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
General sibling (~) Universal

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

Performance Notes

ApproachCostBest for
$("prev ~ siblings")Native CSS when possibleStatic following-sibling queries
$("prev").nextAll("f")Traversal from a setStarting from held elements
$("prev + next")Smaller result setImmediate neighbor only

Prefer a specific prev (id/class) so ~ does not scan more of the tree than you need. Reach for + when one sibling is enough.

Key Takeaways

  • Role: prev ~ siblings selects all following matching siblings.
  • Reach: wider than +; still same-parent siblings only.
  • Skips: nodes before prev, and nested nieces/nephews.
  • Traversal: pair with .nextAll() when you already hold the start node.

One line: $("prev ~ siblings") means “every matching sibling after prev” — not nested, not before.

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: Element Selector

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

Element selector 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