.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
Select a descendant, then collect every ancestor (or only those that match).
Select the starting element(s) — e.g. $("li.item-a").
Call .parents() to collect every ancestor up the tree.
Or pass a selector: .parents("ul") to keep matching ancestors only.
Chain methods on the new set (.append(), .addClass(), …).
Order is closest parent first, then outward; duplicates are removed.
Piece
Required?
Role
Matched set
Yes
Starting element(s)
selector
No
Keep 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)
Reference
Cheat Sheet
Keep these upward methods side by side.
Topic
Detail
Syntax
.parents([selector])
Since
jQuery 1.0
Depth
All 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
Try it
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.
Code
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.
My parents are: STRONG, P, DIV, BODY, HTML
(tags from closest parent outward)
How It Works
Select. Find the b element.
Parents. Collect every ancestor.
Map. Turn each ancestor into its tagName.
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);
});
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.