jQuery .parent() Method

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

What Is .parent()?

.parent([selector]) (since jQuery 1.0) returns the immediate parent of each matched element — one level up. Optional selector keeps the parent only if it matches. Returns a new jQuery set; does not change the DOM.

Remember
$("li.item-a").parent();              // wrapping ul
$("p").parent(".selected");           // parent only if .selected
$(this).parent();                     // common in click handlers

// parent()   → one level up (direct parent)
// parents()  → all ancestors
// closest()  → nearest match (incl. self)
// children() → one level down
NeedUse
Direct parent$el.parent()
All ancestors.parents()
Nearest match (incl. self).closest()
Direct children.children()
Positioned ancestor.offsetParent()

How to Use .parent()

Select a child, then step up one level to its wrapper.

  1. Select the child element(s) — e.g. $("li.item-a").
  2. Call .parent() to get each one’s immediate parent.
  3. Or pass a selector: .parent(".selected") to filter.
  4. Chain methods on the new set (.append(), .addClass(), …).
  5. If the parent fails the filter, the result is empty for that start — no further climb.
PieceRequired?Role
Matched setYesChild element(s)
selectorNoKeep parent only if it matches
Depth—Always exactly one level up
Return value—New jQuery set (0 or 1 per start, after unique)

Pseudocode

Pseudocode
call $(children).parent(selector?)

result = empty set
for each child in the matched set:
    p = child.parentElement
    if p is null:
        skip
    else if no selector OR p matches selector:
        add p to result

return unique parents as a new jQuery object
(never climbs to grandparents)

Cheat Sheet

Keep these upward methods side by side.

TopicDetail
Syntax.parent([selector])
SincejQuery 1.0
DepthOne level up only
vs .parents()All ancestors
vs .closest()Nearest match; tests self first
$("html").parent()Contains document (API note)
DOM change?No — query only

Live Preview

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

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

parent → level-2 ul only. parents("ul") → level-2 and outer list.

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

Official idea: from li.item-a, mark the wrapping ul.

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

How It Works

  1. Start. Select li.item-a.
  2. Up one. Immediate parent is the inner ul.
  3. Stop. No further ancestors are visited.
  4. Mark. Only that wrapping list is tagged.

Example 2 — Prepend Each Element’s Parent Tag

Official Ex. 1 idea: show PARENT > before each element in body.

jQuery
$("*", document.body).each(function () {
  var parentTag = $(this).parent().get(0).tagName;
  $(this).prepend(document.createTextNode(parentTag + " > "));
});
Try It Yourself

How It Works

  1. Scan. Every element under document.body.
  2. Parent. Read that node’s immediate parent tag.
  3. Prepend. Insert TAG > as text at the start.
  4. Learn. Makes the tree relationship visible in the page.

Example 3 — Parent With Class selected

Official Ex. 2 idea: keep the parent only when it has class selected.

jQuery
$("p").parent(".selected").append(" [selected parent]");
Try It Yourself

How It Works

  1. Children. Select every p.
  2. Up. Look at each paragraph’s immediate parent.
  3. Filter. Keep the parent only if it matches .selected.
  4. No climb. Non-matching parents are dropped — not grandparents.

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

Same start — one wrapper versus every ul ancestor.

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

How It Works

  1. parent. Stops at the direct wrapping list.
  2. parents. Collects every matching ul upward.
  3. Count. Often 1 vs 2+ in nested menus.
  4. Pick. Use parent when you mean “my wrapper.”

Example 5 — Highlight the Direct Parent on Click

Click a button — mark its immediate .btn-group parent.

jQuery
$(".toolbar").on("click", "button", function () {
  $(".btn-group").find("[data-mark]").remove();
  $(this).parent().append(" [active]");
});
Try It Yourself

How It Works

  1. Delegate. Listen for clicks on buttons inside the toolbar.
  2. Clear. Remove previous marks.
  3. Parent. $(this).parent() is the wrapping group.
  4. Mark. Only that one-level parent is tagged.

Edge Cases & Pitfalls

Common surprises when the parent “isn’t there.”

filter

Failed selector → empty, not grandparent

.parent(".x") does not keep climbing. Use .closest(".x") or .parents(".x").

html

$("html").parent() is special

Official docs: it returns a set containing document, while $("html").parents() is empty.

unique

Shared parents are unique’d

Several siblings → one parent in the result set, not duplicates.

closest

Need self-or-ancestor?

.parent() never matches the start element. Prefer .closest().

detached

Detached nodes may have no parent

Elements not in the document can return an empty parent set.

Browser Support

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

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
.parent() Universal

Bottom line: The simplest upward step. Use .parents() / .closest() when you need more than one level.

Performance Notes

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

Prefer .parent() when the structure is fixed — one hop is enough.

Key Takeaways

  • Role: immediate parent — one level up — since jQuery 1.0.
  • Filter: optional selector; no match → empty, not climb.
  • vs parents / closest: one hop vs many / nearest match.
  • Mirror: .children() is the one-level-down counterpart.

One line: .parent() is a single step up to the element that wraps you.

Frequently Asked Questions

.parent() returns the immediate parent element of each node in the current collection — one level up the DOM tree. An optional selector filters the parent so it is included only when it matches.
.parent() moves up exactly one level — the direct parent only. .parents() collects every ancestor up to the document root. From a nested li, .parent() gives the containing ul; .parents("ul") can return multiple ul ancestors.
.parent() returns only the immediate parent — no further. .closest(selector) walks up from the element itself (including self) until the first selector match. Use .parent() for the direct parent; use .closest() when the match may be higher or include self.
.parent() goes up one level; .children() goes down one level. They are mirror methods — parent is who wraps you; children are what you directly wrap.
With .parent(selector), the immediate parent is tested. If it does not match, that start contributes nothing — jQuery does not walk to grandparents. Use .parents(selector) or .closest(selector) for a higher ancestor.
No. Like other jQuery traversing methods, it only queries the existing DOM and returns a new collection.

Next: .parents() Method

Walk up and collect every ancestor — not just the immediate parent.

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