Javascript DOM Relation

All nodes in a document have relationships to other nodes.

For the following html document.

        <title>Sample Page</title>

        <p>Hello World!</p>

  • body element is considered a child of the html element.
  • html element is considered the parent of the body element.
  • head element is considered a sibling of the body element.


Each node has a childNodes property containing a NodeList.

A NodeList is an array-like object used to store an ordered list of nodes that are accessible by position.

DOM structure changes will be reflected in NodeList objects automatically.

The following example shows how nodes stored in a NodeList may be accessed via bracket notation or by using the item() method:

var firstChild = someNode.childNodes[0];
var secondChild = someNode.childNodes.item(1);
var count = someNode.childNodes.length;

The length property indicates the number of nodes in the NodeList at that time.

        <pre id="results"><code><code></pre> 
            var resultsElement = document.getElementById("results"); 
            var firstChild = resultsElement.childNodes[0];  
            <!--   w  ww .ja  v  a 2  s. c  o  m-->
            var secondChild = resultsElement.childNodes.item(1); 

Click to view the demo


Each node has a parentNode property pointing to its parent in the document tree.

All nodes contained within a childNodes list have the same parent.

Each node within a childNodes list is considered to be a sibling of the other nodes.

We can navigate from one node in the list to another by using the previousSibling and nextSibling properties.

The first node in the list has null for the value of its previousSibling property, and the last node in the list has null for the value of its nextSibling property.

If there's only one child node, both nextSibling and previousSibling will be null.

The firstChild and lastChild properties point to the first and last node in the childNodes list, respectively.

The value of someNode.firstChild is always equal to someNode.childNodes[0], and the value of someNode.lastChild is always equal to someNode.childNodes[someNode.childNodes.length-1].

If there is only one child node, firstChild and lastChild point to the same node;

If there are no children, then firstChild and lastChild are both null.

hasChildNodes() returns true if the node has one or more child nodes.

The ownerDocument property is a pointer to the document node that represents the entire document.


<!DOCTYPE html>
        <div id="div1">
            <p>Hello <b>World!</b></p>
                <li>List item 1</li>
                <li>List item 2</li>
                <li>List item 3</li>
        <p>Hello <b>World!</b></p>
        <script type="text/javascript">
               var div = document.getElementById("div1");
               var node = div.parentNode;
               <!--from   www.  j  a v a 2 s  .co m-->
               var preNode = div.previousSibling;

               if (preNode.nextSibling === null){
                   document.writeln("Last node in the parent's childNodes list."); 
               } else if (preNode.previousSibling === null){
                   document.writeln("First node in the parent's childNodes list."); 
               var firstNode = div.firstChild;
               var sameNode = div.childNodes[0];
               if (firstNode === sameNode){
               } else {
               var lastNode = div.lastChild;
               sameNode = div.childNodes[div.childNodes.length-1];
               if (lastNode === sameNode){
               } else {

Click to view the demo