jQuery .prevUntil() Method

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

What Is .prevUntil()?

.prevUntil() (since jQuery 1.4) gets all preceding siblings up to but not including a stop (selector, DOM node, or jQuery object). An optional filter keeps matching siblings only.

Remember
// Previous siblings until a dt (stop excluded)
$("#term-2").prevUntil("dt");

// Stop at a DOM node; keep only dd (since 1.6)
$("#term-3").prevUntil(document.getElementById("term-1"), "dd");

// No stop → same as .prevAll()
$("#term-2").prevUntil();
NeedUse
Previous until a stop.prevUntil(stop)
All previous siblings.prevAll()
One step back.prev()
Following until a stop.nextUntil()
Either side.siblings()

How to Use .prevUntil()

Start from an element, walk backward, stop before the fence.

  1. Select the starting element(s).
  2. Call .prevUntil(stop) — selector (1.4) or Element/jQuery (1.6).
  3. Optional: second argument filters collected siblings.
  4. Remember: the stop is excluded; order is nearest first.
  5. If stop is missing/unmatched, you get all previous siblings.
SignatureSinceRole
.prevUntil([selector][, filter])1.4Stop at selector; optional filter
.prevUntil([element][, filter])1.6Stop at Element or jQuery object
No stop / no match—Same as .prevAll() (unfiltered)

Pseudocode

Pseudocode
result = empty jQuery object

for each matched element:
    sibling = immediate previous element sibling
    while sibling exists:
        if sibling matches stop:
            break   // stop excluded
        if no filter OR sibling matches filter:
            append sibling to result   // nearest first
        sibling = previous element sibling of sibling

return result

Cheat Sheet

Keep these facts handy while ranging previous siblings.

TopicDetail
DirectionBackward until a stop
StopExcluded from the result
OrderNearest first
Filter (2nd arg)Keeps matching collected siblings only
vs .prevAll()All previous vs until a fence
vs .nextUntil()Mirror — forward until a stop
No stopBehaves like .prevAll()

Live Preview

From term 2 / term 3, collect previous siblings until a stop.

Markers use text — no CSS required.

term 1
definition 1-a
definition 1-b
term 2
definition 2-a
definition 2-b
term 3
definition 3-a
Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Prefer text markers so demos stay CSS-free.

Example 1 — Previous Until a Selector

Official idea: from term 2, collect siblings until the previous dt.

jQuery
$("#term-2").prevUntil("dt").prepend("[until] ");
// marks definition 1-b and 1-a; term 1 is not marked
Try It Yourself

How It Works

  1. Start. Select #term-2.
  2. Walk. Move to each previous sibling.
  3. Stop. Halt when a dt is reached — exclude it.
  4. Mark. Label only the collected siblings.

Example 2 — Stop Plus Filter

Second argument keeps only matching siblings (e.g. dd).

jQuery
$("#term-3").prevUntil("#term-1", "dd").prepend("[dd] ");
// until term 1; keep dd only — skips term 2
Try It Yourself

How It Works

  1. Stop. Halt before #term-1.
  2. Filter. Keep only siblings matching dd.
  3. Skip. dt / other tags between are not kept.
  4. Use. Range + type filter in one call.

Example 3 — Stop at a DOM Node

Since 1.6, pass an Element (or jQuery object) as the stop.

jQuery
var term1 = document.getElementById("term-1");
$("#term-3").prevUntil(term1, "dd").prepend("[node] ");
// same range as selector stop, with a live Element
Try It Yourself

How It Works

  1. Get node. Resolve the stop Element once.
  2. Pass it. First argument can be Element or jQuery.
  3. Filter. Optional second arg still works.
  4. Handy. When you already hold a DOM reference.

Example 4 — No Stop Means All Previous

Missing or unmatched stop selects every previous sibling — like .prevAll().

jQuery
var a = $("#term-2").prevUntil().map(function () {
  return this.tagName;
}).get().join(", ");

var b = $("#term-2").prevAll().map(function () {
  return this.tagName;
}).get().join(", ");

$("#out").text("until: " + a + " | all: " + b);
// same tag list
Try It Yourself

How It Works

  1. No stop. Walk all the way back.
  2. Same set. Matches unfiltered .prevAll().
  3. Unmatched stop. Same behavior if the selector never hits.
  4. Prefer. Use .prevAll() when you mean “all previous.”

Example 5 — .prevUntil() vs .prevAll()

A fence vs every previous sibling from the same start.

jQuery
var until = $("#term-2").prevUntil("dt").map(function () {
  return $(this).text();
}).get().join(" | ");

var all = $("#term-2").prevAll().map(function () {
  return $(this).text();
}).get().join(" | ");

$("#out").text("until: " + until + " || all: " + all);
Try It Yourself

How It Works

  1. until. Stops before the previous dt.
  2. all. Includes that dt (term 1) too.
  3. Choose. Need a fence → .prevUntil(); all → .prevAll().
  4. One step. Use .prev() for a single neighbor.

Edge Cases & Pitfalls

These trip up beginners most often with .prevUntil().

stop

Stop is never included

The fence is excluded. To include it, chain .addBack() or add the stop separately.

miss

Unmatched stop → all previous

If the stop selector never matches, you get the same set as .prevAll(). Double-check the fence.

order

Nearest-first order

Same reverse order as .prevAll(). Use .uniqueSort() before wrap/append if document order matters.

filter

Filter ≠ stop

First arg is the fence; second arg filters collected siblings. Swapping them changes the meaning.

Browser Support

.prevUntil() has been part of jQuery since 1.4. Passing an Element or jQuery object as the stop requires 1.6+. Works wherever your loaded jQuery build runs.

✓ Stable · jQuery 1.4+

jQuery .prevUntil()

Universal across browsers that run your jQuery version. Prefer current jQuery 3.x+ for teaching and production.

100% jQuery core
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
.prevUntil() Universal

Bottom line: Safe for ranging previous siblings until a fence. Remember: stop excluded, nearest first.

Performance Notes

ApproachCostBest for
.prev()CheapestOne adjacent sibling
.prevUntil(stop)CheapBounded previous range
.prevAll() on long listsWatchPrefer .prevUntil() when a stop exists

A stop bounds the walk. Prefer .prevUntil() over .prevAll() when you only need siblings before a known fence.

Key Takeaways

  • Role: previous siblings up to — but not including — a stop.
  • Filter: second arg keeps matching collected siblings only.
  • Order: nearest first — same as .prevAll().
  • Choose: fence → .prevUntil() · all → .prevAll() · one → .prev().

One line: .prevUntil() walks previous siblings until a fence — stop excluded, optional filter, nearest first.

Frequently Asked Questions

.prevUntil() returns all preceding siblings up to but not including a stop element (selector, DOM node, or jQuery object). Optional second argument filters the collected siblings. Available since jQuery 1.4; element/jQuery stop since 1.6.
No. The stop is a fence — siblings before it are collected; the stop itself is excluded.
.prevAll() takes every previous sibling. .prevUntil(stop) stops when it reaches the stop. If the stop is missing or not supplied, .prevUntil() selects the same set as .prevAll() with no filter.
Nearest first (closest previous sibling to farthest) — the same reverse document order as .prevAll().
The second argument keeps only collected siblings that match that selector — e.g. .prevUntil("#term-1", "dd") keeps dd elements only.
Yes, since jQuery 1.6. Pass an Element or jQuery object as the first argument: .prevUntil(document.getElementById("term-1")).

Next: .siblings() Method

Get all siblings of each matched element — either side.

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

15 people found this page helpful