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
Read the parent Element, null-check, then use Element APIs.
Select a node (element, text, …).
Read node.parentElement.
If it is not null, use Element features (style, classList, …).
Use parentNode if you may need Document / Fragment parents.
Piece
Required?
Role
node
Yes
Any DOM Node
Return
—
Parent Element, or null
Null check
Usually
Avoid 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
Reference
Cheat Sheet
Keep these facts handy when walking up the DOM.
Topic
Detail
Syntax
node.parentElement
Returns
Element or null
Mutates?
No — read-only
vs parentNode
Element-only vs any parent type
html root
parentElement is null
Detached node
null (no parent yet)
Standard
Baseline Widely available (since July 2015)
Try it
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…
Code
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.
Read parentElement. Returns that wrapping Element.
Use Element fields.id and nodeName work.
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");
}
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
One parent read is cheap. For “nearest matching ancestor,” prefer element.closest(selector) over a hand-written climb when you already have an Element.
Remember
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.