jQuery .nextUntil() Method

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

What Is .nextUntil()?

.nextUntil([selector|element] [, filter]) (since jQuery 1.4; DOM/jQuery stop since 1.6) returns following siblings up to — but not including — a stop boundary. An optional second filter keeps only matching elements in that range. Returns a new jQuery object; does not change the DOM.

Remember
$("#term-2").nextUntil("dt");              // dd's until next dt
$("#term-1").nextUntil(term3, "dd");       // dd only, stop at DOM node
$("li.start").nextUntil(".end");           // items before .end
$("li.start").nextUntil();                 // same as .nextAll()

// next()      → one sibling
// nextAll()   → all following (no stop)
// nextUntil() → following until stop (exclusive)
NeedUse
Siblings until a stop$el.nextUntil(stop)
All following (no stop).nextAll()
Immediate next sibling.next()
Backward until a stop.prevUntil()
Filter the range.nextUntil(stop, filter)

How to Use .nextUntil()

Pick a start, choose a stop boundary, optionally filter the range.

  1. Select the starting sibling (e.g. $("#term-2")).
  2. Call .nextUntil("dt") — or pass a DOM node / jQuery object (1.6+).
  3. Optionally add a filter: .nextUntil(stop, "dd").
  4. Remember: the stop element is never in the result.
  5. If the stop is missing, you get the same set as .nextAll().
PieceRequired?Role
Matched setYesStarting element(s)
Stop (selector / node)NoWhere to stop (exclusive). Omit → like .nextAll()
filterNoKeep only matching siblings in the range
Return value—New jQuery set (0 or more)

Pseudocode

Pseudocode
call $(starts).nextUntil(stop?, filter?)

result = empty set
for each start in the matched set:
    sibling = start.nextElementSibling
    while sibling is not null:
        if sibling matches stop:
            break   // stop itself is NOT added
        if no filter OR sibling matches filter:
            add sibling to result
        sibling = sibling.nextElementSibling

return result as a new jQuery object

Cheat Sheet

Keep these forward-sibling methods side by side.

TopicDetail
Syntax.nextUntil([stop] [, filter])
Since1.4 · DOM stop in 1.6
Includes stop?No — exclusive boundary
Includes start?No
vs .nextAll()Bounded range vs all following
vs .next()Range vs one sibling
No stop foundSame as .nextAll()
DOM change?No — query only

Live Preview

From #term-2 in a definition list, compare .nextUntil(), .nextAll(), and a filtered stop.

term 1
definition 1-a
definition 1-b
term 2
definition 2-a
definition 2-b
definition 2-c
term 3
definition 3-a

From term 2: nextUntil("dt") → three dd's; stop at term 3 is excluded.

Result
Ready — choose a call and click Run.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

Example 1 — Official: .nextUntil("dt")

From term 2, mark every sibling until the next term (jQuery API demo).

jQuery
$("#term-2").nextUntil("dt").append(" [until]");
Try It Yourself

How It Works

  1. Start. Select #term-2.
  2. Walk. Collect following siblings one by one.
  3. Stop. Halt when the next dt is reached — do not include it.
  4. Mark. Only the three dd definitions get the suffix.

Example 2 — DOM Stop + "dd" Filter

Official idea: stop at a DOM node and keep only dd siblings.

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

$("#term-1")
  .nextUntil(term3, "dd")
  .append(" [dd]");
Try It Yourself

How It Works

  1. DOM stop. Pass the raw #term-3 element (since 1.6).
  2. Range. Walk from term 1 until that node.
  3. Filter. Keep only siblings matching "dd".
  4. Skip. dt#term-2 is in the range but filtered out.

Example 3 — .nextUntil() vs .nextAll()

Same start — bounded range versus every following sibling.

jQuery
$("#u-term-2").nextUntil("dt").append(" [until]");
$("#a-term-2").nextAll().append(" [all]");
Try It Yourself

How It Works

  1. Until. Stops before the next dt.
  2. All. Keeps walking past later terms.
  3. Same start. Both begin at term 2.
  4. Pick. Use nextUntil when you have a clear end marker.

Example 4 — List Items Until .end

Mark list items between a start and an end class.

jQuery
$("li.start").nextUntil("li.end").append(" [mid]");
Try It Yourself

How It Works

  1. Start. Select li.start.
  2. Stop. Halt at li.end without including it.
  3. Middle. Only items between start and end are marked.
  4. Use. Handy for steppers and section ranges in a list.

Example 5 — Missing Stop Acts Like .nextAll()

If the stop selector never matches, every following sibling is kept.

jQuery
var a = $("li.start").nextUntil(".missing").length;
var b = $("li.start").nextAll().length;

$("#out").text("nextUntil(missing)=" + a + " · nextAll()=" + b);
Try It Yourself

How It Works

  1. Walk. .nextUntil(".missing") looks for a stop that never appears.
  2. Fallback. Official docs: same elements as .nextAll().
  3. Compare. Matching lengths confirm the behavior.
  4. Tip. Double-check your stop selector when results look too large.

Edge Cases & Pitfalls

Why a bounded sibling set can look wrong.

stop

Stop element is never included

Need the boundary too? Select it separately or chain another method.

missing

Missing stop → like .nextAll()

A typo in the stop selector silently returns every following sibling.

parent

Only same-parent siblings

The stop must be a following sibling under the same parent — not a nested child.

filter

Filter skips non-matches in range

.nextUntil(stop, "dd") still walks past other tags; it just does not keep them.

Browser Support

.nextUntil() has been part of jQuery since 1.4. Passing a DOM node or jQuery object as the stop was added in 1.6. It works in every browser that runs your loaded jQuery version.

✓ Stable · jQuery 1.4+

jQuery .nextUntil()

Supported across jQuery 1.x (1.4+), 2.x, and 3.x. Prefer a current jQuery 3.x build. Sibling walks use the live DOM tree.

100% With jQuery
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 for definition lists, steppers, and “until next section” UI. Confirm the stop selector exists when the set looks too large.

Performance Notes

ApproachCostBest for
.next()CheapestOne step forward
.nextUntil(stop)Stops earlyBounded forward range
.nextAll()Linear in all afterRest of the sibling list

Prefer .nextUntil() when you have a clear end marker — it avoids collecting siblings past the section you care about.

Key Takeaways

  • Role: following siblings until a stop — exclusive of the stop.
  • Stop: selector (1.4+) or DOM/jQuery object (1.6+).
  • Filter: optional second argument keeps matching siblings only.
  • Missing stop: same result as .nextAll().

One line: .nextUntil(stop) grabs following siblings up to — but not including — the stop element.

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.
.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 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 start and that boundary are returned.
Yes, since jQuery 1.6. Pass a DOM node or jQuery object as the first argument — for example nextUntil(document.getElementById("term-3")). Traversal stops before that element.
nextUntil(stop, filter) keeps only siblings in the range that also match the filter — useful when mixed sibling types sit between start and stop.
No. Like other jQuery traversing methods, it only queries the existing DOM and returns a new collection.

Next: .prev() Method

Get the immediate previous sibling — the reverse of .next().

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

10 people found this page helpful