jQuery .parent() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Immediate parent

What You’ll Learn

The .parent() traversing method moves up one level in the DOM — it returns the immediate parent of each matched element, optionally filtered by a selector. This tutorial covers the official nested-list and parent-tag demos, comparisons with .parents(), .closest(), and .children(), and practical event-handling patterns.

01

One level

Direct parent

02

Selector

.parent(".x")

03

vs .parents()

One vs all

04

vs .closest()

Not self

05

vs .children()

Up vs down

06

Since 1.0

Core API

Introduction

Every DOM element (except the document root) has a parent — the node that directly contains it. When you need that immediate wrapper — the ul around an li, the div around a clicked span — reach for .parent().

Available since jQuery 1.0, .parent([selector]) constructs a new jQuery object from the direct parent of each element in the current set. It travels exactly one level up — unlike .parents(), which keeps climbing — and unlike .closest(), which can match the element itself or ancestors several levels away.

Understanding the .parent() Method

Given a jQuery object representing a set of DOM elements, .parent() reads each element’s parentNode (for element parents). When you pass a selector, the parent is included in the result only if it matches — otherwise that branch contributes an empty set.

Think of it as the upward counterpart to .children(): children goes down one level; parent goes up one level. For deeper ancestor searches, use .parents() or .closest() instead.

💡
Beginner Tip

$("li.item-a").parent() returns the ul that directly wraps item A — not the outer list, not the body. One step up only.

📝 Syntax

General form of .parent:

jQuery
.parent( [ selector ] )

Parameters

  • selector (optional) — a string containing a selector expression. The immediate parent is included only if it matches this selector.

Return value

  • A new jQuery object containing the immediate parent of each element in the current set (filtered when a selector is provided).

Official jQuery API nested list example

jQuery
$( "li.item-a" ).parent().css( "background-color", "red" );

// Red on the ul that directly contains item A

⚡ Quick Reference

GoalCode
Immediate parent$("span").parent()
Parent only if it is a div$("p").parent("div")
Parent with class selected$("p").parent(".selected")
All ancestors matching$("li").parents("ul")
Nearest matching ancestor (incl. self)$("span").closest(".card")
Direct children (down one level)$("ul").children("li")

📋 .parent() vs .parents() vs .closest() vs .children()

Four tree methods — one level up, all ancestors, nearest match, or one level down.

.parent()
+1 up

Immediate parent only

.parents()
all up

Every ancestor — optional filter

.closest()
first match

Self + ancestors until selector hits

.children()
-1 down

Direct children — mirror of .parent()

Examples Gallery

Examples 1–3 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demos for immediate parent traversal.

Example 1 — Official Demo: Style the Parent of Item “A”

From nested item A, highlight the ul that directly contains it.

jQuery
$( "li.item-a" ).parent().css( "background-color", "red" );
Try It Yourself

How It Works

Item A’s parent is the level-2 ul. .parent() stops there — it does not continue to the outer li or document body.

Example 2 — Official Demo: Prepend Each Element’s Parent Tag Name

Loop every body descendant and label it as parentTag > child.

jQuery
$( "*", document.body ).each(function() {

  var parentTag = $( this ).parent().get( 0 ).tagName;

  $( this ).prepend( document.createTextNode( parentTag + " > " ) );

});
Try It Yourself

How It Works

The official demo uses .parent().get(0).tagName to read the direct parent’s HTML tag — a quick way to visualize the DOM hierarchy for learning.

Example 3 — Official Demo: Parent of p With Class selected

Keep only paragraphs whose immediate parent has class selected, then style that parent.

jQuery
$( "p" ).parent( ".selected" ).css( "background", "yellow" );
Try It Yourself

How It Works

.parent(".selected") tests only the direct parent. If the parent is a plain div without the class, that paragraph contributes nothing — jQuery does not search grandparents.

📈 Practical Patterns

Compare depth with .parents(), then use parent in click handlers.

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

One level up returns one ul; all ancestors can return several.

jQuery
$( "#parent-demo li.item-a" ).parent().css( "outline", "2px solid red" );

$( "#parents-demo li.item-a" ).parents( "ul" ).css( "outline", "2px solid blue" );



// .parent() → 1 ul | .parents("ul") → inner + outer ul
Try It Yourself

How It Works

Nested lists create multiple ul ancestors. .parent() stops at the first; .parents() collects every matching ancestor in the chain.

Example 5 — Highlight the Direct Parent on Click

When a nested control is clicked, style its immediate wrapper — simpler than .closest() when one level is enough.

jQuery
$( ".toolbar" ).on( "click", "button", function() {

  $( this ).parent().addClass( "active-group" );

});
Try It Yourself

How It Works

Each button’s direct parent is often a div.btn-group. .parent() targets that wrapper without climbing to the whole toolbar — use .closest() when you need a higher container.

🚀 Common Use Cases

  • Highlight containing list$("li.active").parent("ul").addClass("has-active").
  • Form field wrapper$("input:invalid").parent(".field").addClass("error").
  • Remove parent row$("input:checked").parent("tr").remove() (when checkbox is direct child).
  • DOM debugging — official pattern: prepend parent tag name to each node.
  • Delegated click styling — style the immediate parent of the clicked child.
  • Filter by parent type$("span").parent("div") keeps spans whose direct parent is a div.

🧠 How .parent() Moves Up One Level

1

Start with source elements

jQuery object holds one or more DOM nodes.

Input
2

Read parentNode

Get the immediate parent for each element.

Traverse
3

Apply selector filter

If provided, keep parent only when it matches.

Filter
4

Return & chain

New jQuery object — one parent per source element.

📝 Notes

  • Available since jQuery 1.0 — optional selector argument filters the direct parent.
  • Travels exactly one level up — not the same as .parents() or .closest().
  • Does not include the starting element — only its parent (unlike .closest()).
  • $("html").parent() returns document; $("html").parents() is empty.
  • With a selector, only the immediate parent is tested — grandparents are not checked.
  • Pushes onto jQuery’s internal stack — pair with .end() to restore the previous set.

Browser Support

.parent() has been part of jQuery since 1.0+. It relies on standard parent-node traversal with no browser-specific behavior beyond jQuery itself.

jQuery 1.0+

jQuery .parent()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: element.parentElement — jQuery adds selector filtering and collection semantics.

100% With jQuery loaded
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: Safe in any jQuery project. Use .parent() for one step up; use .parents() or .closest() when you need higher ancestors.

Conclusion

The jQuery .parent() method returns the immediate parent of each element in the current collection. It is the simplest way to step up one level in the DOM — the natural counterpart to .children().

Remember the official list lesson: from item A, the inner ul gets the red background. Use .parents() when you need every ancestor, .closest() when the match may be several levels up or include self, and .parent(selector) when only the direct parent matters.

💡 Best Practices

✅ Do

  • Use .parent() when the direct wrapper is what you need
  • Pair mentally with .children() — opposite directions
  • Use .parent(selector) to require a specific parent type
  • Switch to .closest() when the target container is not the immediate parent
  • Call .end() after .parent() when continuing on the original set

❌ Don’t

  • Use .parent() when you need all ancestors — use .parents()
  • Expect .parent(".card") to skip non-matching parents
  • Confuse parent with offset parent — use .offsetParent() for positioning context
  • Assume parent returns an element node for text nodes — parent can be a document fragment in edge cases
  • Use .parent() when .closest() is clearer for event delegation

Key Takeaways

Knowledge Unlocked

Five things to remember about .parent()

One step up to the direct parent.

5
Core concepts
02

.children()

Mirror

Compare
↑↑ 03

.parents()

All up

Compare
? 04

Selector

Direct only

Filter
html 05

Edge

→ document

Note

❓ Frequently Asked Questions

.parent() returns the immediate parent element of each node in the current jQuery 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') could return multiple ul ancestors at different depths.
.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 matching ancestor may be several levels up or include the element itself.
.parent() goes up one level; .children() goes down one level. They are mirror-image methods — parent returns who wraps you; children returns what you directly wrap.
When you call .parent(selector), the immediate parent is tested against the selector. If it does not match, that element contributes nothing to the result — jQuery does not keep walking to grandparents. Use .parents(selector).first() or .closest(selector) when you need a higher ancestor.
It returns the document node — unlike $('html').parents(), which returns an empty jQuery object. This edge case shows .parent() always moves exactly one step up, even at the top of the tree.
Did you know?

jQuery’s .parent() has existed since version 1.0 — among the earliest traversing methods. The optional selector parameter lets you express “give me the parent only if it is a div” without writing .parent().filter("div"), keeping ancestor hops readable in event handlers and animation chains.

Back to jQuery Traversing

Explore more DOM traversal methods as the collection grows.

Traversing hub →

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.

6 people found this page helpful