TOC PREV NEXT INDEX

domref



parentNode


Returns the parent of the current node.

Syntax

node = element.parentNode
 

Parameters

node is a node object.

Example

As you can see from the following not very interesting example, even simple HTML documents can contain a complex hierarchy of parents and children. In this case, the document object is a parent of the HTML object, which is a parent of the BODY object, which in turn is a parent of the H1 object being examined.

// alerts: 9 for Document object
 
<html>
 
<head>
 
<script>
 
function init() {
 
  h1 = document.createElement('H1');
 
  t = document.createTextNode("heading 1");
 
  h1.appendChild(t);
 
  bod = document.getElementById("b");
 
  bod.appendChild(h1);
 
}
 

 
function findParent() {
 
  h1 = document.getElementsByTagName("H1");
 
  alert(h1[0].parentNode.parentNode.parentNode.nodeName);
 
}
 
</script>
 
</head>
 
<body id="b" onload="init();">
 
<form><input type="button" value="find parent" onclick="findParent();" /></form>
 
</body>
 
</html>
 

Notes

Note that this property returns NULL for the document itself, but can be used on the children of the document to refer back to the document or other intermediate parents (see example above).

Specification

parentNode
 


Netscape Communications
http://developer.netscape.com
TOC PREV NEXT INDEX