jQuery Next Siblings Selector (“prev ~ siblings”)

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
All following siblings

What You’ll Learn

The next siblings selector uses the ~ combinator to match every following sibling that shares the same parent — not just the immediate next one. Available since jQuery 1.0; ideal for section paragraphs after headings, validation hints after error markers, and styling groups of sibling elements.

01

Syntax

prev ~ siblings

02

General

All following siblings

03

Official

#prev ~ div

04

vs +

Adjacent sibling

05

Same parent

Shared parent required

06

CSS

Standard combinator

Introduction

Siblings share the same parent. When you need to style or manipulate every element that comes after a reference sibling — not just the one directly next to it — the general sibling combinator ~ is the right tool.

Official jQuery documentation describes prev ~ siblings as selecting all sibling elements that follow after prev, have the same parent, and match the filtering siblings selector. This is broader than the adjacent sibling combinator +, which stops at the immediate next sibling. This tutorial walks through the official #prev ~ div demo, compares reach with +, and shows practical patterns for typography, forms, and validation UI.

Understanding the Next Siblings Selector

Think of prev ~ siblings as a sibling chain relationship: “Is this element a following sibling that comes after that element?”

  • h2 ~ p → all paragraphs after each h2 — not just the lead paragraph.
  • #prev ~ div → every div sibling after #prev; span siblings are skipped.
  • .error ~ .help-text → every help hint that follows an error marker sibling.
💡
Beginner Tip

Elements between prev and a matching sibling do not break a ~ match — unlike +, which requires immediate adjacency. Nested children (like a div inside a sibling) are not siblings themselves, so they are never matched by ~.

📝 Syntax

Official jQuery API form (since 1.0):

jQuery
jQuery( "prev ~ siblings" )

// common patterns:
$( "#prev ~ div" )
$( "h2 ~ p" )
$( "label ~ input" )
$( ".error ~ .help-text" )

Parameters

  • prev — any valid selector for the preceding sibling element(s).
  • siblings — a selector to filter following siblings of each matched prev.

Return value

  • A jQuery object containing every following sibling that matches siblings after each matched prev.
  • Can match zero, one, or many elements per prev — unlike +, which matches at most one.

Official jQuery API example

jQuery
// Borders all div siblings after #prev
$( "#prev ~ div" ).css( "border", "3px groove blue" );

Official markup pattern: div before #prev (no match), span#prev, then div and span siblings — only following div siblings receive the border. Nested divs inside a sibling (“niece”) are descendants, not siblings.

⚡ Quick Reference

Sibling relationshipprev + nextprev ~ siblings
Immediate next siblingMatchesMatches
Second or later following siblingNo matchMatches
Other element between prev and targetNo match (for that pair)Still matches if target is a later sibling
Nested inside prev (descendant)No matchNo match
Different parentNo matchNo match

📋 General Sibling (~) vs Related Combinators

Same DOM tree — different relationship rules.

General sibling
h2 ~ p

All following siblings

Adjacent
h2 + p

Immediate next sibling only

Child
section > p

Direct child of parent

Descendant
section p

Nested at any depth

.nextAll()
$("h2").nextAll("p")

Traversal equivalent

Examples Gallery

Example 1 follows the official jQuery #prev ~ div demo. Examples 2–5 style all section paragraphs, compare + vs ~ reach, show why gaps do not break ~, and highlight validation hints after error markers. Use the Try-it links to run each snippet.

📚 Official jQuery Demo

Border all div siblings after #prev — official API markup pattern.

Example 1 — Official Demo: #prev ~ div

Official jQuery demo — add a blue groove border to every div sibling after #prev.

jQuery
$( "#prev ~ div" ).css( "border", "3px groove blue" );

// Matches div siblings after #prev
// Skips: div before #prev, span siblings, nested "niece" divs
Try It Yourself

How It Works

Each matched div must appear after #prev among siblings with the same parent. The selector filters by tag name — span siblings are skipped even though they follow #prev.

Example 2 — Style All Section Paragraphs: h2 ~ p

Apply section styling to every paragraph that follows each h2.

jQuery
$( "h2 ~ p" ).css( "background", "#e0f2fe" ).css( "padding", "8px" );

// All paragraphs after each h2 — not just the lead paragraph
Try It Yourself

How It Works

h2 ~ p picks every paragraph sibling after each heading. Use h2 + p instead when you only want the lead paragraph directly after the heading.

📈 Practical Patterns

Reach comparison, gaps between siblings, and validation UI patterns.

Example 3 — Reach Comparison: h2 + p vs h2 ~ p

Count how many paragraphs each combinator matches when several follow one heading.

jQuery
console.log( "Adjacent (+):", $( "h2 + p" ).length );
console.log( "General (~):", $( "h2 ~ p" ).length );

// h2 + p = one lead paragraph per h2
// h2 ~ p = every paragraph sibling after each h2
Try It Yourself

How It Works

The notable difference between + and ~ is reach. + stops at the immediate next sibling; ~ continues through every following sibling that matches the filter.

Example 4 — Gaps Don’t Break ~: label ~ input

Show that elements between siblings do not prevent a general sibling match.

jQuery
$( "label ~ input" ).addClass( "general-match" );
$( "label + input" ).addClass( "adjacent-match" );

// label → span → input: ~ matches, + does not
// label → input: both + and ~ match
Try It Yourself

How It Works

~ only requires that the target comes after prev among siblings. Other elements in between do not break the chain — a key difference from +.

Example 5 — Validation Hints: .error ~ .help-text

Reveal every help hint that follows an error marker sibling.

jQuery
$( ".error ~ .help-text" ).addClass( "visible" );

// Every .help-text sibling after .error in the same field group
Try It Yourself

How It Works

When validation markup places an error marker before multiple hint elements, ~ selects all of them in one query — without chaining multiple + selectors.

🚀 Common Use Cases

  • Section typography — style all paragraphs after each heading with h2 ~ p.
  • Form fields — target every input that follows a label sibling, even with hint text between.
  • Validation UI — show all help hints after an .error marker.
  • Tab panels — reveal every panel sibling after an active tab control.
  • List styling — highlight all items after a separator or category heading sibling.
  • Accordion content — animate every content block that follows an expanded header sibling.

🧠 How jQuery Evaluates prev ~ siblings

1

Find prev matches

jQuery first matches every element that satisfies the prev part of the selector.

Prev
2

Scan following siblings

For each matched prev, walk every sibling element that comes after it in document order.

Following
3

Filter by siblings

Keep siblings only if they match the siblings selector and share the same parent as prev.

Filter
4

Return collection

All matched following siblings become a jQuery object for chaining.

📝 Notes

  • Available since jQuery 1.0 — standard CSS general sibling combinator syntax.
  • Both prev and matched siblings must share the same parent element.
  • Matches all following siblings — not just the immediate next one (use + for that).
  • Elements between prev and a target sibling do not break a ~ match.
  • Nested descendants inside siblings are not siblings themselves — they are never matched by ~.
  • Equivalent traversal: $("prev").nextAll("siblings").

Browser Support

The ~ general sibling combinator is standard CSS and works in jQuery 1.0+. Modern browsers support document.querySelectorAll("h2 ~ p") natively. jQuery provides consistent cross-browser behavior for sibling selection.

CSS · jQuery 1.0+

jQuery Next Siblings Selector ("~")

Supported in all modern browsers. Native: document.querySelectorAll("prev ~ siblings"). All following siblings — not just the immediate next.

100% Standard CSS
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
~ combinator Universal

Bottom line: Use ~ when you need every following sibling after prev. Prefer .nextAll() when you already hold a jQuery collection.

Conclusion

The next siblings selector prev ~ siblings matches every following sibling that satisfies your filter — a flexible tool for section paragraphs, form fields separated by hints, and validation UI. The official #prev ~ div demo shows the pattern: only div siblings after #prev receive the border.

When you need only the immediate next sibling, switch to +. For parent-child or nested relationships, use > or a space. Traversal methods like .nextAll() offer the same general-sibling logic from an existing jQuery object.

💡 Best Practices

✅ Do

  • Use ~ when multiple siblings follow a reference element
  • Compare ~ vs + before choosing a combinator
  • Scope with a parent class or ID to avoid matching across sections
  • Use .nextAll() when starting from a jQuery object
  • Test with realistic markup gaps in Try-it examples

❌ Don’t

  • Assume ~ and + behave the same
  • Use ~ when you only need the immediate next sibling
  • Expect nested descendants to match — only siblings qualify
  • Forget that both elements must share the same parent
  • Confuse general sibling (~) with descendant (space) matching

Key Takeaways

Knowledge Unlocked

Five things to remember about the general sibling selector

All following siblings — not just the immediate next one.

5
Core concepts
#p 02

Official

#prev ~ div

Demo
+ 03

Adjacent

One sibling

Compare
gap 04

Gaps OK

Not immediate

Pattern
.nA 05

.nextAll()

Traversal twin

API

❓ 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 just the first one.
+ matches only the single element directly after prev. ~ matches every following sibling that satisfies siblings. $("h2 + p") picks one lead paragraph per h2; $("h2 ~ p") picks all paragraphs after each h2 until a new section breaks the sibling chain.
A space matches nested descendants at any depth inside an ancestor. ~ matches siblings at the same level — both elements must share the same parent. $("div p") finds paragraphs inside divs; $("h2 ~ p") finds paragraphs that are siblings after h2, not nested inside it.
Official docs use $("#prev ~ div").css("border", "3px groove blue") on markup with div and span siblings after #prev — all following div siblings get a border; span siblings and nested "niece" divs are skipped.
Both target following siblings. $("prev ~ siblings") is a CSS selector — great for one-shot queries from the document root. .nextAll("siblings") is a jQuery traversal method — useful when you already hold a jQuery object and want every following sibling that matches a filter.
Yes. The ~ combinator is standard CSS and works in jQuery 1.0+. Native document.querySelectorAll("h2 ~ p") uses the same syntax in modern browsers.
Did you know?

The ~ combinator can match many following siblings per prev — if three paragraphs follow an h2, all three match h2 ~ p. The adjacent combinator + would match only the first.

Continue to Element Selector

After general sibling (~) matching, learn how to select elements by tag name with the element selector.

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.

6 people found this page helpful