In JavaScript, understanding the difference between parentNode
and parentElement
is crucial for effective DOM manipulation. Both properties are used to access the parent of a given node, but they differ in what they return:
parentNode
: Returns the parent node of any node type, including elements, text nodes, and comments.parentElement
: Returns only the parent element node, and will return null
if the parent is not an element.Grasping these distinctions helps developers navigate and manipulate the DOM more precisely, ensuring they target the correct nodes in their scripts.
The parentNode
property in the DOM returns the parent node of a specified node. It is a read-only property.
Syntax:
node.parentNode
Return Value:
null
if the node has no parent.Types of Nodes Returned:
The parentElement
property in the DOM returns the parent element node of a specified element. If the parent node is not an element node, it returns null
.
Syntax:
node.parentElement
Return Value:
null
if the parent is not an element node.parentNode
and parentElement
parentNode
: Returns the parent node of the specified node, which can be any node type (element, text, comment, etc.). If there is no parent, it returns null
.parentElement
: Returns the parent element of the specified node. If the parent is not an element node, it returns null
.parentNode
: Can reference any type of node, including element nodes, text nodes, and comment nodes.parentElement
: Only references element nodes. If the parent is not an element node, it returns null
.Using parentNode
:
<div>
<p id="example">Hello, World!</p>
</div>
<script>
var node = document.getElementById("example");
console.log(node.parentNode.nodeName); // Outputs: DIV
</script>
Using parentElement
:
<div>
<p id="example">Hello, World!</p>
</div>
<script>
var node = document.getElementById("example");
console.log(node.parentElement.nodeName); // Outputs: DIV
</script>
Difference in behavior:
<html>
<body>
<script>
console.log(document.documentElement.parentNode); // Outputs: #document
console.log(document.documentElement.parentElement); // Outputs: null
</script>
</body>
</html>
In the last example, document.documentElement
(which is the <html>
element) has a parent node (#document
), but no parent element, hence parentElement
returns null
.
parentNode
Scenario: When you need to access any type of parent node, including non-element nodes like text nodes or document nodes.
Example:
<div>
<p id="text">Hello, World!</p>
</div>
<script>
const textNode = document.getElementById("text").firstChild;
console.log(textNode.parentNode.nodeName); // Outputs: P
</script>
Here, parentNode
is used to get the parent of a text node, which is the <p>
element.
parentElement
Scenario: When you specifically need to access the parent element node and ensure the parent is an element.
Example:
<ul>
<li id="item">Item 1</li>
</ul>
<script>
const listItem = document.getElementById("item");
console.log(listItem.parentElement.nodeName); // Outputs: UL
</script>
In this case, parentElement
is used to get the parent element of the <li>
element, which is the <ul>
element.
parentNode
returns any parent node, including document nodes and text nodes.parentElement
returns only element nodes and will return null
if the parent is not an element.The `parentNode` property returns any parent node, including document nodes and text nodes, while the `parentElement` property returns only element nodes and will return `null` if the parent is not an element.
This distinction is crucial when working with DOM nodes to ensure that you are accessing the correct type of parent node based on your specific use case.
When you need to access any type of parent node, including non-element nodes like text nodes or document nodes, use `parentNode`. However, when you specifically need to access the parent element node and ensure the parent is an element, use `parentElement`.