.parentsUntil([stop] [, filter]) (since jQuery 1.4) returns ancestors of each matched element up to but not including a stop selector, DOM node, or jQuery object (node/jQuery stop since 1.6). Optional filter keeps only matching ancestors in that range. Returns a new jQuery set; does not change the DOM.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("li.item-a").parentsUntil(".level-1");
$("li.item-2").parentsUntil("ul.level-1", ".yes");
$("button").parentsUntil($("#panel")); // stop at jQuery (1.6+)
// parentsUntil(stop) → ancestors before stop
// parents() → all ancestors
// parent() → one level up
// closest() → nearest match (incl. self)
Call
What it does
Matched set
Starting element(s)
stop
Selector / Element / jQuery — climb until here (excluded)
filter
Keep only matching ancestors in the range
Return value
New jQuery set (unique ancestors before stop)
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Select the starting element(s) — e.g. $("li.item-a").
Step 2
Call .parentsUntil(stop) with a selector, Element, or jQuery object.
Step 3
Optionally pass a second filter selector to keep matching ancestors only.
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefined
Getters usually return a value from the first matched element, or undefined when nothing applies.
jQuery Examples
Example 1 — Ancestors Until .level-1
Official idea: from li.item-a, mark ancestors before the outer list.
.parentsUntil → stops before .level-1
.parents → continues to body / html
How It Works
until. Stops climbing at the boundary.
parents. Keeps going to the root.
Count. Fewer nodes with a clear stop.
Frequently Asked Questions
.parentsUntil() collects ancestors of each matched element up to — but not including — a stop selector, DOM node, or jQuery object. An optional second filter keeps only ancestors that match.
.parents() walks all the way toward the document root (optionally filtered). .parentsUntil(stop) stops before the stop match, so outer wrappers past that point are excluded.
No. The stop match is the boundary and is never included. Ancestors between the start and that boundary are returned.
If the stop is omitted or not found, .parentsUntil() behaves like .parents() with no selector — every ancestor is selected.
Yes, since jQuery 1.6. The first argument can be a selector string, a DOM Element, or a jQuery object. The optional second argument is always a filter selector.
No. Like other jQuery traversing methods, it only queries the existing DOM and returns a new collection.