JavaScript Node parentElement Property

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

What Is parentElement?

node.parentElement returns the parent Element of a node, or null if there is no parent or the parent is not an Element (MDN). Use parentNode when you need any parent type (including Document).

Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. Read-only — change structure with append/remove APIs, not by assigning here.

Remember
node.parentElement
→ Element parent
→ or null (no parent / parent is not an Element)

parentElement → Element only
parentNode    → any parent (Element, Document, Fragment, …)

Classic:
  html.parentElement → null
  html.parentNode    → document
GoalPrefer
Parent tag (Element)node.parentElement
Any parent nodenode.parentNode
Owning Documentnode.ownerDocument

How to Use parentElement

Read the parent Element, null-check, then use Element APIs.

  1. Select a node (element, text, …).
  2. Read node.parentElement.
  3. If it is not null, use Element features (style, classList, …).
  4. Use parentNode if you may need Document / Fragment parents.
PieceRequired?Role
nodeYesAny DOM Node
Return—Parent Element, or null
Null checkUsuallyAvoid reading properties on null

Pseudocode

Pseudocode
parent = node.parentElement

if parent is null:
  // no Element parent (detached, or parent is Document)
else:
  // safe Element APIs
  parent.style.color = "red"

// climb Element ancestors:
el = node
while el:
  process(el)
  el = el.parentElement

// vs parentNode:
html.parentElement → null
html.parentNode    → document

Cheat Sheet

Keep these facts handy when walking up the DOM.

TopicDetail
Syntaxnode.parentElement
ReturnsElement or null
Mutates?No — read-only
vs parentNodeElement-only vs any parent type
html rootparentElement is null
Detached nodenull (no parent yet)
StandardBaseline Widely available (since July 2015)

Live Preview

Compare parentElement / parentNode, style the parent, or climb ancestors.

Demo
Hi

Starts from #pe-child. Styling uses the Element style property (MDN demo).

Result
Choose an action, then Run…

JavaScript Examples

Short, copy-ready snippets based on MDN Node.parentElement. Use Try It Yourself for a live editor.

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

  1. Select the child. Nested inside a div.
  2. Read parentElement. Returns that wrapping Element.
  3. Use Element fields. id and nodeName work.
  4. Same as parentNode here because the parent is an Element.

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

  1. Guard with if. Avoids errors when parent is null.
  2. Use Element.style. Only Elements have this API.
  3. Matches MDN’s sample of coloring the parent.
  4. Why parentElement helps: you already know the result is an Element (or null).

Example 3 — HTML Root: 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

  1. html’s parent is Document. Not an Element.
  2. parentElement filters to Elements. Result is null.
  3. parentNode still works. Returns the Document.
  4. Climbing stops here when looping with parentElement.

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

  1. createElement alone. Node is not in the tree.
  2. No parent slot. Both parent APIs are null.
  3. isConnected is false. Confirms detachment.
  4. Append to attach and give it a parent Element.

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

  1. Start at the child. Push each nodeName.
  2. Move with parentElement. Only Element parents.
  3. Stop at null. HTML’s Element parent is null.
  4. Document is skipped — never appears in this climb.

Edge Cases & Pitfalls

Check these before assuming every node has an Element parent.

null

html.parentElement is null

MDN: the Document parent is not an Element. Use parentNode if you need the Document.

detached

Unappended nodes have no parent

Fresh createElement results return null until you append them.

vs

parentNode is broader

Prefer parentElement when you need Element APIs; use parentNode for Document / Fragment parents.

guard

Always null-check before styling

Reading .style on null throws. Follow MDN: if (node.parentElement) { … }.

Browser Support

Node.parentElement is marked Baseline Widely available on MDN (since July 2015).

✓ Baseline · Widely available

Node.parentElement

Returns the parent Element or null. Prefer over parentNode when you need Element-only parents. Safe everyday DOM API.

100% Widely available
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
parentElement Excellent

Bottom line: Safe everyday DOM API. Remember html → null. See the MDN parentElement reference.

Performance Notes

ApproachCostBest for
node.parentElementO(1)One step up to an Element
Climb with whileO(depth)Ancestor walks
closest(selector)O(depth)Find matching ancestor Element

One parent read is cheap. For “nearest matching ancestor,” prefer element.closest(selector) over a hand-written climb when you already have an Element.

Key Takeaways

  • Parent Element: or null if none / not an Element.
  • vs parentNode: Element-only vs any parent type.
  • html → null: Document parent is skipped.
  • Null-check: then use Element APIs like style.

One line: parentElement is the parent tag — or null when the parent is missing or is the Document.

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.

Next: parentNode

Learn the broader parent pointer that can return Document too.

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

9 people found this page helpful