Difference between DOM parentNode and parentElement
Difference between DOM parentNode and parentElement
Can somebody in explain me in as simple as possible terms, what is the difference between classical DOM parentNode and newly introduced in Firefox 9 parentElement
bugzilla.mozilla.org/show_bug.cgi?id=685798
– user1106925
Dec 31 '11 at 2:00
parentNode seems to be DOM standard, so it is safer always use it instead of parentElement.
– TMS
Oct 11 '13 at 19:15
@TMS w3school is not an authority: w3fools.com
– Guillaume Massé
Jul 25 '15 at 18:56
5 Answers
5
parentElement is new to Firefox 9 and to DOM4, but it has been present in all other major browsers for ages.
parentElement
In most cases, it is the same as parentNode. The only difference comes when a node's parentNode is not an element. If so, parentElement is null.
parentNode
parentNode
parentElement
null
As an example:
document.body.parentNode; // the <html> element
document.body.parentElement; // the <html> element
document.documentElement.parentNode; // the document node
document.documentElement.parentElement; // null
(document.documentElement.parentNode === document); // true
(document.documentElement.parentElement === document); // false
Since the <html> element (document.documentElement) doesn't have a parent that is an element, parentElement is null. (There are other, more unlikely, cases where parentElement could be null, but you'll probably never come across them.)
<html>
document.documentElement
parentElement
null
parentElement
null
In other words, it's completely pointless 99.999999999999% of the time. Whose idea was it?
– Niet the Dark Absol
Dec 31 '11 at 2:33
The original
parentElement was a proprietary IE thing; I believe other browsers at the time (e.g., Netscape) supported parentNode but not parentElement. (Obviously, given I've mentioned Netscape, I'm talking about way back to IE5 and earlier...)– nnnnnn
Dec 31 '11 at 3:04
parentElement
parentNode
parentElement
@lonesomeday you forgot
documentfragment.firstChild.parentElement === null– Raynos
Jan 4 '12 at 14:38
documentfragment.firstChild.parentElement === null
@Raynos That was actually the precise circumstance I had in mind with the last sentence of my answer...
– lonesomeday
Jan 4 '12 at 16:08
As I have just discovered, on an SVG element (like a
circle inside a g), in IE, parentElement will be undefined, and parentNode will be what you're looking for. :(– Jason Walton
Mar 25 '15 at 3:27
circle
g
parentElement
parentNode
In Internet Explorer, parentElement is undefined for SVG elements, whereas parentNode is defined.
parentElement
parentNode
honestly I think this is more of a comment rather than an answer.
– shabunc
Mar 28 '16 at 20:15
Probably, but it's the reason I banged my head against the table for an hour or more until I figured it out. I suspect many others come to this page after a similar head-banging.
– speedplane
Apr 21 '16 at 20:54
@speedplane Glad this is an answer as this makes no logical sense and had me stumped for a good while...
– superphonic
Apr 5 '17 at 13:03
Edit: Some of this is wrong. See comments below for details
All Element objects are also Node objects (because Element is a descendent of Node). But there is a Node which isn't an Element... the document object. So your <html> element has a parent node (document) but it doesn't have a parent element.
Element
Node
Element
Node
Node
Element
document
<html>
document
The reason that there's a need for parentElement instead of parentNode is because, HTML5 doesn't strictly require an <html> element, so almost any element can have a parent node without actually having a parent element. So if my HTML page looked like this:
parentElement
parentNode
<html>
<!doctype html>
<title>My page</title>
<header>This is my page</header>
<div id="body">
<p>This is some text from my page</p>
</div>
<footer>
Copyright, me
</footer>
Then the title, header, #body and footer elements would have their parentNode as document, but their parentElement would be null. Only the p tag would have a parentElement, which is #body. (Note that I wouldn't advise this as a page structure... stick to something more traditional.)
title
header
#body
footer
parentNode
document
parentElement
p
parentElement
#body
You can replicate it with something like this:
if (myElement.parentNode instanceof Element)
myElement.parentElement = myElement.parentNode;
else
myElement.parentElement = null;
"because, HTML5 doesn't strictly require an
<html> element" Not true, you've misinterpreted the HTML5 specification. The html tag is optional, the element is not, it's always there whether the tag is implied or explicitly provided. Details in Section 8 of the spec.– T.J. Crowder
Jan 1 '12 at 18:54
<html>
html
@Nathan T.J. is right. If you actually look at your markup using something like software.hixie.ch/utilities/js/live-dom-viewer you'll see that the parser in fact infers
<html> and <body> elements in that case. You can create situations where some random tag that's not <html> is a child of the document, but you have to do it with script (in HTML documents; for random XML this is not true), and it takes some work.– Boris Zbarsky
Jan 1 '12 at 19:40
<html>
<body>
<html>
Ah, I see. Yes, I have mis-interpreted, having read that bit a long time ago.
– Nathan MacInnes
Jan 2 '12 at 0:14
There is more than one node that isn't an element. There are 12 different node types, only one of which is an element.
– RobG
Nov 2 '15 at 14:11
@NathanMacInnes, Rubbish. The code proves that parentNode is defined: jsfiddle.net/fg3pkgrw
– Pacerier
Sep 28 '17 at 14:50
Use .parentElement and you can't go wrong as long as you aren't using document fragments.
.parentElement
If you use document fragments, then you need .parentNode:
.parentNode
let div = document.createDocumentFragment().appendChild(document.createElement('div'));
div.parentElement // null
div.parentNode // document fragment
Also:
let div = document.getElementById('t').content.firstChild
div.parentElement // null
div.parentNode // document fragment
<template id=t><div></div></template>
Apparently the <html>'s .parentNode links to the Document. This should be considered a decision phail as documents ain't Nodes since Nodes are defined to be containable by documents and documents can't be contained by documents.
<html>
.parentNode
Just like with nextSibling and nextElementSibling, just remember that, properties with "element" in their name always returns Element or null. Properties without can return any other kind of node.
Element
null
console.log(document.body.parentNode, "is body's parent node"); // returns <html>
console.log(document.body.parentElement, "is body's parent element"); // returns <html>
var html = document.body.parentElement;
console.log(html.parentNode, "is html's parent node"); // returns document
console.log(html.parentElement, "is html's parent element"); // returns null
yeah, but unlike nextsibling text or comment nodes can't be parent.
– Jasen
Sep 4 '17 at 2:25
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
stackoverflow.com/questions/2899196/what-is-this-parentelement
– user1106925
Dec 31 '11 at 1:57