jQuery .parentsUntil() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.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)
CallWhat it does
Matched setStarting element(s)
stopSelector / Element / jQuery — climb until here (excluded)
filterKeep only matching ancestors in the range
Return valueNew jQuery set (unique ancestors before stop)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the starting element(s) — e.g. $("li.item-a").
Step 2Call .parentsUntil(stop) with a selector, Element, or jQuery object.
Step 3Optionally pass a second filter selector to keep matching ancestors only.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters 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.

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.

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.

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.

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: .closest() Method

Continue with jQuery utility helpers after the traversing parent chain.

.closest() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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