jQuery .parents() Method

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

What Is .parents()?

.parents([selector]) (since jQuery 1.0) returns all ancestors of each matched element — from the immediate parent outward toward the document root. Optional selector keeps only ancestors that match. Returns a new jQuery set; does not change the DOM.

Remember
$("li.item-a").parents();              // all ancestors
$("li.item-a").parents("ul");          // only ul ancestors
$("span").parents("div");              // every wrapping div

// parents()  → all ancestors
// parent()   → one level up
// closest()  → nearest match (incl. self)
// parentsUntil() → ancestors until a stop
NeedUse
All ancestors$el.parents()
Filtered ancestors$el.parents("ul")
Direct parent only.parent()
Nearest match (incl. self).closest()
Ancestors until a stop.parentsUntil()

How to Use .parents()

Select a descendant, then collect every ancestor (or only those that match).

  1. Select the starting element(s) — e.g. $("li.item-a").
  2. Call .parents() to collect every ancestor up the tree.
  3. Or pass a selector: .parents("ul") to keep matching ancestors only.
  4. Chain methods on the new set (.append(), .addClass(), …).
  5. Order is closest parent first, then outward; duplicates are removed.
PieceRequired?Role
Matched setYesStarting element(s)
selectorNoKeep only matching ancestors
Depth—Walks up to the document root
Return value—New jQuery set (unique, closest-first)

Pseudocode

Pseudocode
call $(starts).parents(selector?)

result = empty set
for each start in the matched set:
    node = start.parentElement
    while node is an element:
        if no selector OR node matches selector:
            add node to result
        node = node.parentElement

return unique ancestors as a new jQuery object
(ordered closest parent → outermost)

Cheat Sheet

Keep these upward methods side by side.

TopicDetail
Syntax.parents([selector])
SincejQuery 1.0
DepthAll ancestors (to root)
vs .parent()One level only
vs .closest()First match; tests self first
$("html").parents()Empty set (API note)
DOM change?No — query only

Live Preview

From li.item-a, compare .parents(), .parents("ul"), .parent(), and .closest("ul").

  • I
  • II
    • A
    • B
    • C
  • III

parents → full chain. parents("ul") → every ul. parent → one hop. closest("ul") → nearest ul (incl. self if ul).

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 — All Ancestors of Item “A”

Official idea: from li.item-a, mark every ancestor up the tree.

jQuery
$("li.item-a").parents().append(" [ancestor]");
Try It Yourself

How It Works

  1. Start. Select li.item-a.
  2. Climb. Walk from the immediate parent to the root.
  3. Collect. Every ancestor is in the result set.
  4. Mark. Each ancestor is tagged with text.

Example 2 — List Ancestor Tag Names of Each b

Official Ex. 1 idea: map .parents() to tag names and append them.

jQuery
var parentEls = $("b").parents()
  .map(function () {
    return this.tagName;
  })
  .get()
  .join(", ");
$("b").append(" " + parentEls);
Try It Yourself

How It Works

  1. Select. Find the b element.
  2. Parents. Collect every ancestor.
  3. Map. Turn each ancestor into its tagName.
  4. Show. Join the names and append beside the bold text.

Example 3 — Unique div Parents of Selected Spans

Official Ex. 2 idea: click spans, then count unique div ancestors.

jQuery
$("span").on("click", function () {
  $(this).toggleClass("selected");
  var len = $("span.selected").parents("div").length;
  $("b").text("Unique div parents: " + len);
});
Try It Yourself

How It Works

  1. Toggle. Click adds or removes selected on a span.
  2. Filter. .parents("div") keeps only div ancestors.
  3. Unique. Shared wrappers appear once in the set.
  4. Report. Show the length in the b element.

Example 4 — .parent() vs .parents()

Same start — one wrapper versus the full ancestor chain.

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

How It Works

  1. parent. Stops at the direct wrapping list.
  2. parents. Continues through every ancestor.
  3. Count. Often 1 vs many in nested menus.
  4. Pick. Use parents when you need the whole chain.

Example 5 — Filter Ancestors With .parents("ul")

Keep only ul ancestors — skip li, body, and the rest.

jQuery
$("li.item-a").parents("ul").append(" [ul]");
Try It Yourself

How It Works

  1. Start. Select li.item-a.
  2. Climb. Visit every ancestor.
  3. Filter. Keep only nodes that match ul.
  4. Mark. Those list wrappers are tagged.

Edge Cases & Pitfalls

Common surprises when collecting ancestors.

html

$("html").parents() is empty

Official docs: $("html").parents() returns empty, while $("html").parent() contains document.

self

Start element is never included

.parents() starts at the parent. Need self-or-ancestor? Use .closest().

unique

Shared ancestors are unique’d

Several starts under the same wrappers → each ancestor once in the result.

order

Closest parent first

Results go outward. With multiple starts, original order is reversed and duplicates removed.

filter

Empty filter result is normal

.parents(".missing") may be empty even when ancestors exist — they just don’t match.

Browser Support

.parents() has been part of jQuery since 1.0. It works in every browser that runs your loaded jQuery version — no separate browser feature flag.

✓ Stable · jQuery 1.0+

jQuery .parents()

Supported across jQuery 1.x, 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
.parents() Universal

Bottom line: Collects the full ancestor chain. Prefer .parent() for one hop, .closest() for the nearest match.

Performance Notes

ApproachCostBest for
.parent()Cheapest upwardKnown direct wrapper
.closest(sel)Stops at first matchUnknown depth / self match
.parents()Walks to rootCollecting every ancestor
.parents(sel)Walk + filterSeveral matching ancestors

Prefer .closest() when you need one match. Use .parents() when you truly need the chain.

Key Takeaways

  • Role: all ancestors — closest parent first — since jQuery 1.0.
  • Filter: optional selector keeps matching ancestors only.
  • vs parent / closest: full chain vs one hop / nearest match.
  • Stop early: .parentsUntil() when you want a cutoff.

One line: .parents() collects every ancestor from your wrapper up to the root.

Frequently Asked Questions

.parents() returns every ancestor of each element in the current collection — walking up from the immediate parent toward the document root. An optional selector keeps only ancestors that match.
.parent() moves up exactly one level. .parents() keeps climbing and collects every ancestor (optionally filtered). From a nested li, .parent() gives one ul; .parents("ul") can return multiple ul ancestors.
.parents() returns all matching ancestors (or every ancestor with no selector). .closest(selector) stops at the first match and includes the starting element itself. Use .closest() for one nearest match; use .parents() for the full chain.
For one start element, ancestors are ordered closest parent first, then outward to html. With multiple starts, jQuery reverses the original order and removes duplicates.
Only matching ancestors are kept. If none match, you get an empty jQuery object — safe to chain. Use .closest(selector) when you want the first match at any depth.
No. Like other jQuery traversing methods, it only queries the existing DOM and returns a new collection.

Next: .parentsUntil() Method

Collect ancestors until you hit a stop selector — not all the way to the root.

.parentsUntil() tutorial →

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