.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.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
// 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();
Call
What it does
.prevUntil([selector][, filter])
Stop at selector; optional filter
.prevUntil([element][, filter])
Stop at Element or jQuery object
No stop / no match
Same as .prevAll() (unfiltered)
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
[selector][
Used in .prevUntil([selector][, filter]). Stop at selector; optional filter
filter]
Used in .prevUntil([selector][, filter]). Stop at selector; optional filter
[element][
Used in .prevUntil([element][, filter]). Stop at Element or jQuery object
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
jQuery Examples
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.
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
.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")).