This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

parentNode

Summary

Retrieves the parent node in the document hierarchy.

Property of dom/Nodedom/Node

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