jQuery .nextUntil() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Siblings until stop

What You’ll Learn

The .nextUntil() traversing method collects following siblings up to a stop boundary — every sibling after the current element until (but not including) a matched selector, DOM node, or jQuery object. This tutorial covers the official definition-list demo, DOM-node stop with filter, comparisons with .nextAll(), and practical section-range patterns.

01

Stop boundary

Up to, not including

02

Selector stop

.nextUntil("dt")

03

DOM stop

Since jQuery 1.6

04

Filter arg

, "dd"

05

vs .nextAll()

Range vs all

06

Since 1.4

Core API

Introduction

Sometimes you need siblings in a range — definitions under one term but not the next, list items until a separator, or table rows until a header row. .nextAll() grabs everything ahead; .nextUntil() stops at a boundary you define.

Available since jQuery 1.4 (DOM-node stop since 1.6), .nextUntil(stop [, filter]) walks forward through following siblings and halts before the first match of the stop argument. An optional filter narrows which siblings within that range are kept — ideal when terms and definitions share the same parent as mixed sibling types.

Understanding the .nextUntil() Method

Given a jQuery object representing a set of DOM elements, .nextUntil() examines each element’s following siblings in order. Siblings are added to the result until jQuery reaches one that matches the stop selector, DOM node, or jQuery object — that stop sibling is excluded. Siblings after the stop are never reached.

Think of it as a sliding window among siblings: start just after your element, end right before the stop. Without a stop or when the stop is never found, the window extends to the end — equivalent to .nextAll(). The second filter argument lets you keep only certain tags or classes within that window.

💡
Beginner Tip

$("#term-2").nextUntil("dt") selects definition dd elements for term 2 only — it stops before the next dt (term 3). The stop dt itself is never selected.

📝 Syntax

General forms of .nextUntil:

jQuery
.nextUntil( [ selector ] [, filter ] )

.nextUntil( [ element ] [, filter ] )

Parameters

  • selector — a string containing a selector expression indicating where to stop matching following siblings (stop element excluded).
  • element — a DOM node or jQuery object indicating the stop boundary (since jQuery 1.6).
  • filter (optional) — a selector expression; only siblings in the range that match this filter are included.

Return value

  • A new jQuery object containing following siblings up to but not including the stop match, optionally filtered.

Official jQuery API definition-list example

jQuery
$( "#term-2" ).nextUntil( "dt" ).css( "background-color", "red" );

// Red on definition 2-a, 2-b, 2-c — stops before term-3 dt

⚡ Quick Reference

GoalCode
Following siblings until next dt$("#term-2").nextUntil("dt")
Stop at a DOM node, filter to dd$("#term-1").nextUntil(term3, "dd")
All following siblings (no stop)$("li").nextAll()
Items until a separator li$("li.start").nextUntil("li.separator")
Rows until a header tr$("tr.data").nextUntil("tr.section-head")
Mirror going backward$("li").prevUntil("li.marker")

📋 .nextUntil() vs .nextAll() vs .next()

Three forward sibling methods — one step, full tail, or bounded range.

.nextUntil()
until stop

Following siblings up to boundary — stop excluded

.nextAll()
all ahead

Every following sibling — no stop point

.next()
+1 only

One immediately following sibling

.prevUntil()
backward

Same range logic — preceding siblings until stop

Examples Gallery

Examples 1–2 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demos for bounded sibling ranges.

Example 1 — Official Demo: Definitions Until the Next dt

From term 2, highlight sibling definitions — stop before the next term heading.

jQuery
$( "#term-2" ).nextUntil( "dt" ).css( "background-color", "red" );
Try It Yourself

How It Works

After #term-2, jQuery collects siblings until the next dt (#term-3). Those three dd elements are in range; the stop dt is excluded.

Example 2 — Official Demo: Stop at DOM Node with dd Filter

From term 1, green text on every dd until (not including) term 3.

jQuery
var term3 = document.getElementById( "term-3" );

$( "#term-1" ).nextUntil( term3, "dd" ).css( "color", "green" );
Try It Yourself

How It Works

The DOM node #term-3 is the stop boundary. The "dd" filter keeps only definitions in the range — intermediate dt elements are skipped because they do not match the filter.

📈 Practical Patterns

Compare with .nextAll(), then apply stop boundaries in lists and tables.

Example 3 — .nextUntil() vs .nextAll() on the Same List

See how a stop boundary limits the range compared to selecting the full tail.

jQuery
$( "#until-demo li.third-item" ).nextUntil( "li.stop" ).css( "color", "red" );

$( "#all-demo li.third-item" ).nextAll().css( "color", "blue" );



// .nextUntil("li.stop") → items 4 only | .nextAll() → items 4 and 5
Try It Yourself

How It Works

Item 5 has class stop. .nextUntil("li.stop") from item 3 collects only item 4; item 5 is the excluded boundary. .nextAll() ignores the boundary and takes both.

Example 4 — List Items Until a Separator

Hide or style siblings in one section without affecting the next group.

jQuery
$( "li.section-start" ).nextUntil( "li.section-end" )

  .addClass( "in-section" );
Try It Yourself

How It Works

Section markers act as start and stop bookends. .nextUntil() selects the content between them — the end marker itself is excluded from the result.

Example 5 — Table Rows Until a Section Header

Stripe or highlight data rows belonging to one group before the next header row.

jQuery
$( "tr.group-head" ).nextUntil( "tr.group-head" )

  .addClass( "group-row" );
Try It Yourself

How It Works

Each header row starts a range; the next header is the stop. Data rows between two tr.group-head elements get the class — a common grouped-table pattern without nested markup.

🚀 Common Use Cases

  • Definition lists$("dt.term").nextUntil("dt") for definitions under one term.
  • Sectioned lists — items between .section-start and .section-end markers.
  • Grouped table rows — data rows until the next tr.group-head.
  • Wizard steps — steps in the current phase until a .phase-divider sibling.
  • Mixed sibling filter.nextUntil(stop, "dd") when only one tag type matters in the range.
  • DOM-node boundary — stop at a known element reference from JavaScript.

🧠 How .nextUntil() Builds a Sibling Range

1

Start with source elements

jQuery object holds one or more DOM nodes.

Input
2

Walk forward sibling by sibling

Collect each following element sibling in order.

Traverse
3

Stop before boundary match

Halt when stop selector, DOM node, or jQuery object matches — exclude it.

Boundary
4

Filter & return

Apply optional filter — return bounded sibling set for chaining.

📝 Notes

  • Available since jQuery 1.4 — DOM-node or jQuery stop since 1.6.
  • The stop element is never included — only siblings strictly before it in the chain.
  • If the stop is not found, behavior matches .nextAll() (with optional filter).
  • The optional filter applies to siblings within the range — not to the stop element.
  • Text nodes and comments between elements are skipped — element siblings only.
  • Mirror method: .prevUntil() — same range logic going backward.

Browser Support

.nextUntil() has been part of jQuery since 1.4+ (DOM stop since 1.6). It relies on standard sibling traversal with no browser-specific behavior beyond jQuery itself.

jQuery 1.4+

jQuery .nextUntil()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. No native single-method equivalent — combine nextElementSibling loops or use .nextUntil() for clarity.

100% With jQuery loaded
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
.nextUntil() Universal

Bottom line: Safe in any jQuery project. Use .nextUntil() for bounded sibling ranges; use .nextAll() when no stop boundary exists.

Conclusion

The jQuery .nextUntil() method returns following siblings up to but not including a stop boundary. It fills the gap between .next() (one step) and .nextAll() (everything ahead) when your DOM has natural section markers.

Remember the official definition-list lesson: $("#term-2").nextUntil("dt") styles only term 2’s definitions. Pass a DOM node as the stop and a filter selector when mixed sibling types share the same parent.

💡 Best Practices

✅ Do

  • Use clear stop markers in markup — dt, .section-end, header rows
  • Pass a filter when only one sibling type belongs in the range
  • Remember the stop element is excluded from results
  • Use DOM-node stop when you already hold an element reference
  • Compare with .nextAll() — range vs full tail

❌ Don’t

  • Expect the stop boundary inside the returned set
  • Use .nextUntil() when you need every following sibling — use .nextAll()
  • Assume the filter searches beyond the stop — range ends at the boundary
  • Confuse siblings with descendants — nested content needs .find()
  • Forget that a missing stop falls back to .nextAll() behavior

Key Takeaways

Knowledge Unlocked

Five things to remember about .nextUntil()

Following siblings with a stop boundary.

5
Core concepts
02

Stop

Excluded

Boundary
→→ 03

.nextAll()

No stop

Compare
dd 04

Filter

2nd arg

Filter
DOM 05

Node stop

Since 1.6

API

❓ Frequently Asked Questions

.nextUntil() returns following siblings of each element in the current set, stopping before the first sibling that matches a selector, DOM node, or jQuery object. The stop element itself is never included. It defines a bounded range of siblings ahead — not the entire tail.
.nextAll() returns every following sibling with no stop point. .nextUntil(stop) returns siblings only until (but not including) the stop match. If no stop is supplied or the stop is never found, .nextUntil() behaves like .nextAll().
No. The element matched by the stop selector, DOM node, or jQuery object is excluded. Only siblings between the starting element and that stop boundary are returned.
Yes, since jQuery 1.6. Pass a raw DOM node or jQuery object as the first argument — for example nextUntil(document.getElementById('term-3')). The traversal stops before that element.
nextUntil(stop, filter) limits which siblings in the range are kept. Only elements between start and stop that also match the filter selector are included — useful when mixed sibling types sit in the same range.
jQuery collects all following siblings — the same result as .nextAll() with no filter. The range runs to the end of the sibling list because no stop boundary was encountered.
Did you know?

When jQuery added DOM-node and jQuery-object support to .nextUntil() in version 1.6, it matched the same upgrade given to .add(), .filter(), and other methods — letting you stop at an element you already selected instead of writing a new selector string for the boundary.

Back to jQuery Traversing

Explore more DOM traversal methods as the collection grows.

Traversing hub →

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