.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();
Start from an element, walk backward, stop before the fence.
Select the starting element(s).
Call .prevUntil(stop) — selector (1.4) or Element/jQuery (1.6).
Optional: second argument filters collected siblings.
Remember: the stop is excluded; order is nearest first.
If stop is missing/unmatched, you get all previous siblings.
Signature
Since
Role
.prevUntil([selector][, filter])
1.4
Stop at selector; optional filter
.prevUntil([element][, filter])
1.6
Stop 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
Reference
Cheat Sheet
Keep these facts handy while ranging previous siblings.
Topic
Detail
Direction
Backward until a stop
Stop
Excluded from the result
Order
Nearest 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 stop
Behaves like .prevAll()
Try it
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.
Code
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
…definition 1-* and 2-* get [dd]
term 2 is walked past but not kept (not a dd)
How It Works
Stop. Halt before #term-1.
Filter. Keep only siblings matching dd.
Skip.dt / other tags between are not kept.
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
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
until: definition 1-b | definition 1-a
all: definition 1-b | definition 1-a | term 1
How It Works
until. Stops before the previous dt.
all. Includes that dt (term 1) too.
Choose. Need a fence → .prevUntil(); all → .prevAll().
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.prevUntil()Universal
Bottom line: Safe for ranging previous siblings until a fence. Remember: stop excluded, nearest first.
Analysis
Performance Notes
Approach
Cost
Best for
.prev()
Cheapest
One adjacent sibling
.prevUntil(stop)
Cheap
Bounded previous range
.prevAll() on long lists
Watch
Prefer .prevUntil() when a stop exists
A stop bounds the walk. Prefer .prevUntil() over .prevAll() when you only need siblings before a known fence.
Remember
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")).