parentNode
Summary
Retrieves the parent node in the document hierarchy.
Syntax
Note: This property is read-only.
var parentNode = node.parentNode;
Return Value
Returns an object of type DOM NodeDOM Node
The parent node of the node.
Examples
This example assigns the parentNode of a span object to a variable.
<script>
var oParent = document.getElementById("oSpan").parentNode;
</script>
:
<body>
<span ID="oSpan">A Span</span>
</body>
This example assigns the parentNode of a node, created with the createElement method, to a variable.
var oNode = document.createElement("B");
document.body.insertBefore(oNode);
// This returns document.body.
var oParent = oNode.parentNode;
This example demonstrates the difference between parentNode and parentElement when queried on the documents root element (<html>)
<!DOCTYPE html>
<html id="root">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>parentElement/parentNode</title>
</head>
<body>
<script type="text/javascript">
var root=document.getElementById('root');
try{document.write('typeof root.parentNode:'+ typeof(root.parentNode)+'<br/>');}catch(e){alert(e);}
try{document.write('root.parentNode.tagName:'+root.parentNode.tagName+'<br/>');}catch(e){alert(e);}
try{document.write('typeof root.parentElement:'+typeof(root.parentElement)+'<br/>');}catch(e){alert(e);}
try{document.write('root.parentElement.tagName:'+root.parentElement.tagName+'<br/>');}catch(e){alert(e);}
</script>
</body>
</html>
Usage
Use to find the parent of a node (if any).
Notes
The topmost object returns null as its parent.
Related specifications
- DOM Level 3 Core
- Recommendation
Attributions
Mozilla Developer Network : [Node.parentNode Article]
Microsoft Developer Network: [parentNode Property Article]