jQuery .parents() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("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
CallWhat it does
Matched setStarting element(s)
selectorKeep only matching ancestors
DepthWalks up to the document root
Return valueNew jQuery set (unique, closest-first)

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 .parents() to collect every ancestor up the tree.
Step 3Or pass a selector: .parents("ul") 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.

jQuery Examples

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.

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.

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.

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() 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