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
Fundamentals
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.
Concept
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.
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");
}
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
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in modern IE versions
Full support
parentElementExcellent
Bottom line: Use parentElement for Element parents; use parentNode for any parent node type.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about parentElement
Parent Element—or null.
5
Core concepts
👆01
Element parent
or null
API
⚖️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>.