jQuery .parentsUntil() Method

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

What Is .parentsUntil()?

.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.

Remember
$("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)
NeedUse
Ancestors until a stop$el.parentsUntil(stop)
Range + filter$el.parentsUntil(stop, filter)
All ancestors.parents()
Direct parent only.parent()
Nearest match (incl. self).closest()

How to Use .parentsUntil()

Pick a start, choose where to stop climbing, then optionally filter the range.

  1. Select the starting element(s) — e.g. $("li.item-a").
  2. Call .parentsUntil(stop) with a selector, Element, or jQuery object.
  3. Optionally pass a second filter selector to keep matching ancestors only.
  4. Chain methods on the new set (.append(), .addClass(), …).
  5. Remember: the stop match itself is never included.
PieceRequired?Role
Matched setYesStarting element(s)
stopNoSelector / Element / jQuery — climb until here (excluded)
filterNoKeep only matching ancestors in the range
Return value—New jQuery set (unique ancestors before stop)

Pseudocode

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

result = empty set
for each start in the matched set:
    node = start.parentElement
    while node is an element:
        if stop is set AND node matches stop:
            break   // do not include stop
        if no filter OR node matches filter:
            add node to result
        node = node.parentElement

return unique ancestors as a new jQuery object

Cheat Sheet

Keep these upward methods side by side.

TopicDetail
Syntax.parentsUntil([stop] [, filter])
SincejQuery 1.4 (Element/jQuery stop: 1.6)
Stop included?No — boundary only
No stop / no matchSame as .parents()
vs .parents()Full chain vs cut off at stop
Sibling cousins.nextUntil(), .prevUntil()
DOM change?No — query only

Live Preview

From li.item-a, compare .parentsUntil(".level-1"), .parents(), and .parent().

  • I
  • II
    • A
    • B
      • 1
      • 2
      • 3
    • C
  • III

until → ancestors before .level-1. parents → full chain. parent → one hop.

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 — Ancestors Until .level-1

Official idea: from li.item-a, mark ancestors before the outer list.

jQuery
$("li.item-a").parentsUntil(".level-1").append(" [until]");
Try It Yourself

How It Works

  1. Start. Select li.item-a.
  2. Climb. Collect each ancestor upward.
  3. Stop. Halt when .level-1 is reached.
  4. Exclude. The stop list is left unmarked.

Example 2 — Filtered Ancestors With .yes

Official idea: until ul.level-1, keep only ancestors that have class yes.

jQuery
$("li.item-2")
  .parentsUntil($("ul.level-1"), ".yes")
  .append(" [yes]");
Try It Yourself

How It Works

  1. Start. Select li.item-2.
  2. Stop. Climb until the jQuery stop $("ul.level-1").
  3. Filter. Keep ancestors matching .yes only.
  4. Mark. Those filtered nodes get the tag.

Example 3 — .parentsUntil() vs .parents()

Same start — cut off at a stop versus the full ancestor chain.

jQuery
$("#until-demo li.item-a").parentsUntil(".level-1").append(" [until]");
$("#parents-demo li.item-a").parents().append(" [parents]");
Try It Yourself

How It Works

  1. until. Stops climbing at the boundary.
  2. parents. Keeps going to the root.
  3. Count. Fewer nodes with a clear stop.
  4. Pick. Use until when the outer container should stay out.

Example 4 — Stop at a DOM Node (Since jQuery 1.6)

Pass an Element (or jQuery object) as the first argument instead of a selector string.

jQuery
var stop = document.getElementById("panel");
$("#panel button").parentsUntil(stop).append(" [path]");
Try It Yourself

How It Works

  1. Stop node. Grab the panel Element by id.
  2. Start. Select the button inside the panel.
  3. Range. Ancestors before that Element are collected.
  4. Boundary. The panel Element is excluded (1.6+ API).

Example 5 — Highlight Path Within a Section

Click a link — mark ancestors inside the section, not past it.

jQuery
$("section").on("click", "a", function (e) {
  e.preventDefault();
  $("section").find("[data-mark]").remove();
  $(this).parentsUntil("section").append(" [path]");
});
Try It Yourself

How It Works

  1. Delegate. Listen for clicks on links inside the section.
  2. Clear. Remove previous path marks.
  3. Until. Collect ancestors before section.
  4. Mark. Only the in-section path is tagged.

Edge Cases & Pitfalls

Common surprises when stopping the climb early.

stop

Stop match is never included

Need the boundary too? Combine with .addBack() patterns or select the stop separately.

miss

Missing stop → full chain

If the stop is omitted or never found, you get the same set as .parents().

filter

Filter is the second argument

.parentsUntil(stop, filter) — swap them and you change the stop, not the filter.

self

Start element is never included

Climbing begins at the parent. Need self-or-ancestor? Use .closest().

version

Element / jQuery stop needs 1.6+

Selector stop works since 1.4. Passing a DOM node or jQuery object requires jQuery 1.6+.

Browser Support

.parentsUntil() has been part of jQuery since 1.4 (Element/jQuery stop since 1.6). It works in every browser that runs your loaded jQuery version — no separate browser feature flag.

✓ Stable · jQuery 1.4+

jQuery .parentsUntil()

Supported across jQuery 1.x (1.4+), 2.x, and 3.x. Prefer a current jQuery 3.x build. Traversal uses 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
.parentsUntil() Universal

Bottom line: Cut the ancestor chain at a stop. Prefer .parents() for the full climb, .parent() for one hop.

Performance Notes

ApproachCostBest for
.parent()Cheapest upwardKnown direct wrapper
.closest(sel)Stops at first matchOne nearest match / self
.parentsUntil(stop)Walk until stopPath inside a known container
.parents()Walks to rootFull ancestor chain

Prefer .parentsUntil() when you know the outer boundary — fewer nodes than climbing to html.

Key Takeaways

  • Role: ancestors before a stop — since jQuery 1.4.
  • Stop: selector, Element, or jQuery (1.6+) — never included.
  • Filter: optional second arg keeps matching ancestors in range.
  • Fallback: no stop → same as .parents().

One line: .parentsUntil() collects ancestors until a boundary — and leaves that boundary out.

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.

Next: jQuery Utilities

Continue with jQuery utility helpers after the traversing parent chain.

Utilities hub →

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