Document Object Model

HTML or XML document can be represented as a hierarchy of nodes using the DOM.

For the following html code:

 
<html>
    <head>
        <title>Sample Page</title>
    </head>
    <body>
        <p id='myP'>Hello World!</p>
    </body>
</html>
  

Its DOM tree is

domModel.png

The root is document node. The only child of the document node is the <html> element.

<html> element is called the document element. The document element is the outermost element.

p is an HTML element and body is also an HTML element. myP is an attribute.

HTML elements are represented by element nodes. attributes are represented by attribute nodes

Each node has a type. Nodes have node type, attributes have node type and comments have comment type. The element node and attribute are different in term of type.

In total, there are 12 node types, all of which inherit from a base type.

The Node Type

The Node interface is implemented in JavaScript as the Node type. All node types inherit from Node in JavaScript. All node types share the same basic properties and methods.

Every node has a nodeType property that indicates the type of node that it is. Node types are represented by one of the following 12 numeric constants on the Node type:

TypeValue
Node.ELEMENT_NODE1
Node.ATTRIBUTE_NODE2
Node.TEXT_NODE3
Node.CDATA_SECTION_NODE4
Node.ENTITY_REFERENCE_NODE5
Node.ENTITY_NODE6
Node.PROCESSING_INSTRUCTION_NODE7
Node.COMMENT_NODE8
Node.DOCUMENT_NODE9
Node.DOCUMENT_TYPE_NODE10
Node.DOCUMENT_FRAGMENT_NODE11
Node.NOTATION_NODE12

A node's type is easy to determine by comparing against one of these constants:

 
<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Example</title> 

    </head> 
    <body> 
        <pre id="results"></pre> 
        <script> 
            var resultsElement = document.getElementById("results"); 
            document.writeln(resultsElement.nodeType); 
            document.writeln(resultsElement.nodeName); 
            if (resultsElement.nodeType == Node.ELEMENT_NODE){ //won't work in IE < 9
                
               document.writeln("Node is an element."); 
            } 
            
        </script> 
    </body> 
</html>
  
Click to view the demo

For cross-browser compatibility, it's best to compare the nodeType property against a numeric value:

 
<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Example</title> 

    </head> 
    <body> 
        <pre id="results"></pre> 
        <script> 
            var resultsElement = document.getElementById("results"); 
            document.writeln(resultsElement.nodeType); 
            document.writeln(resultsElement.nodeName); 
            if (resultsElement.nodeType == 1){
                
               document.writeln("Node is an element."); 
            } 
            
        </script> 
    </body> 
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    DOM  

DOM Model:
  1. Document Object Model
  2. nodeName and nodeValue Properties
  3. Node Relationships:childNodes
  4. parentNode
  5. previousSibling and nextSibling properties
  6. lastChild and firstChild
  7. appendChild() adds a node to the end of the childNodes list
  8. insertBefore()
  9. ownerDocument property
  10. removeChild
  11. replaceChild()
  12. Working with Text
  13. Check the length of NodeList
  14. Convert NodeList to an Array
  15. html tag and its cooresponding JavaScript class