JavaScript Node parentElement Property

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

What You’ll Learn

The Node.parentElement property returns the parent Element, or null if there is none. Learn how it differs from parentNode, why html.parentElement is null, and how to safely style a parent.

01

Kind

Read-only property

02

Returns

Element or null

03

vs parentNode

Element-only

04

<html>

null

05

Detached

null

06

Status

Baseline widely

Introduction

Most of the time you want the parent tag—a div, section, or li. parentElement gives you that Element directly, so you can set styles or classes without worrying that the parent might be the Document.

When you need every parent kind (including Document), use parentNode instead.

💡
Beginner tip

Always null-check: if (node.parentElement) { … }. Detached nodes and the document root both return null.

Understanding parentElement

MDN: the read-only parentElement property returns the DOM node’s parent Element, or null if the node has no parent or its parent is not a DOM Element. parentNode returns any kind of parent.

  • Returns an Element when the parent is a tag.
  • Returns null for no parent, or a non-Element parent (e.g. Document).
  • Read-only — change structure with DOM methods, not assignment.

📝 Syntax

JavaScript
const parent = node.parentElement;
if (parent) {
  parent.style.color = "red";
}

Return value

An Element, or null if there isn’t one.

⚖️ parentElement vs parentNode

node.parentElementnode.parentNode
ReturnsParent Element or nullAny parent Node or null
Child of a divThat divThat div
document.documentElement (<html>)nulldocument
Detached nodenullnull
Best forUI / styling parentsFull tree walks

⚡ Quick Reference

GoalCode
Get parent elementnode.parentElement
Safe stylenode.parentElement?.style.color = "red";
Any parent typenode.parentNode
Climb one levelel = el.parentElement in a loop
<html> parentdocument.documentElement.parentElementnull
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Node.parentElement.

Returns
Element | null

Parent tag

Baseline
widely

Since July 2015

Access
read-only

No assignment

Non-Element parent
null

e.g. Document

Examples Gallery

Examples follow MDN Node.parentElement patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read the parent element and style it safely.

Example 1 — Read the Parent Element

A child inside a wrapper reports that wrapper as parentElement.

JavaScript
const child = document.getElementById("child");
const parent = child.parentElement;

console.log(parent.id);       // "wrap"
console.log(parent.nodeName); // "DIV"
Try It Yourself

How It Works

Because the parent is an Element (div), parentElement and parentNode both point to the same node here.

Example 2 — Style the Parent (MDN-style)

Null-check, then set a style on the parent element.

JavaScript
const node = document.getElementById("child");

if (node.parentElement) {
  node.parentElement.style.color = "red";
  console.log("Parent text color set to red");
}
Try It Yourself

How It Works

The guard avoids errors when there is no Element parent. Styles then apply to the wrapping tag.

📈 null Cases & Climbing

Document root, detached nodes, and walking up element parents.

Example 3 — <html>: parentElement vs parentNode

MDN’s classic: the document root has a Document parent, not an Element.

JavaScript
const html = document.querySelector("html");

console.log(html.parentElement); // null
console.log(html.parentNode);    // document
Try It Yourself

How It Works

parentElement filters to Elements only. The Document is a Node but not an Element, so you get null here.

Example 4 — Detached Nodes Have No Parent

Created but not appended elements return null.

JavaScript
const orphan = document.createElement("div");

console.log(orphan.parentElement); // null
console.log(orphan.parentNode);    // null
console.log(orphan.isConnected);   // false
Try It Yourself

How It Works

No parent means both parentElement and parentNode are null. Append the node to give it a parent.

Example 5 — Climb Element Parents

Walk upward until there is no more Element parent.

JavaScript
let el = document.getElementById("child");
const path = [];

while (el) {
  path.push(el.nodeName);
  el = el.parentElement;
}

console.log(path.join(" → ")); // e.g. SPAN → DIV → BODY → HTML
Try It Yourself

How It Works

The loop stops at <html> because its parentElement is null (the Document is skipped).

🚀 Common Use Cases

  • Style / class a wrapper — toggle classes on the parent Element.
  • Event helpers — from event.target, reach the nearest parent tag.
  • Climb for layout — walk parentElement until a matching selector.
  • Avoid Document surprises — prefer over parentNode when you need Element APIs.
  • Teach the DOM root — show why html.parentElement is null.

🧠 How parentElement Works

1

Look at the parent slot

Every node may have a parent in the tree (or none).

Parent
2

Ask: is it an Element?

Tags yes; Document / Fragment no.

Filter
3

Return Element or null

Ready for style, classList, and other Element APIs.

Result
4

Your script branches

Null-check, then style, climb, or fall back to parentNode.

📝 Notes

Universal Browser Support

Node.parentElement is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Node.parentElement

Safe for production. Prefer it when you need an Element parent; use parentNode when Document parents matter.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in modern IE versions
Full support
parentElement Excellent

Bottom line: Use parentElement for Element parents; use parentNode for any parent node type.

Conclusion

Node.parentElement returns the parent Element or null. Prefer it for UI work; use parentNode when you need Document (or other non-Element) parents. Always null-check before styling.

Continue with parentNode, ownerDocument, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Null-check before using parentElement
  • Use it when you need Element APIs (style, classList)
  • Climb with a while (el) loop for ancestor paths
  • Compare with parentNode when teaching the document root
  • Consider closest() for selector-based ancestors

❌ Don’t

  • Assume every node has an Element parent
  • Assign to parentElement (read-only)
  • Confuse null on <html> with a bug
  • Forget detached nodes also return null
  • Confuse DOM Node with the Node.js runtime

Key Takeaways

Knowledge Unlocked

Five things to remember about parentElement

Parent Element—or null.

5
Core concepts
⚖️ 02

vs parentNode

Element-only

Compare
🚫 03

html → null

Document parent

Gotcha
🎨 04

Style safely

null-check first

Pattern
🔀 05

Climb up

while parentElement

Walk

❓ Frequently Asked Questions

The parent Element of the node, or null if there is no parent or the parent is not an Element (for example, the parent is a Document).
No. MDN marks Node.parentElement as Baseline Widely available (since July 2015). It is a standard DOM property — not Deprecated, Experimental, or Non-standard.
parentNode returns any kind of parent node (Element, Document, DocumentFragment, …). parentElement returns only an Element, or null when the parent is not an Element.
The <html> element’s parent is the Document, which is not an Element. So parentElement is null while parentNode is the document.
When the node has no parent at all — for example a freshly created element that has not been appended yet.
Yes. You read it to find the parent element; you do not assign to it. Use appendChild / remove / replaceWith to change structure.
Did you know?

MDN’s html.parentElement // null vs html.parentNode // document example is one of the clearest ways to see that Documents are nodes but not elements—and why Element-only APIs stop at <html>.

Next: parentNode

Get any parent Node—including Document—or null.

parentNode →

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.

5 people found this page helpful