Javascript DOM

document object modal
JAVASCRIPT Document Object Model ( DOM )

Document Object Model or DOM is a Tree Like Structure of HTML Document, with connected nodes, as shown above. Everything on a webpage page is treated as a node, i.e. HTML tags, text inside, attributes and child tag. HTML Tag, (<HTML>) is the Root Node, and all other elements are child node of html tag.


 <h1 class="heading"> Javascript <small> DOM </small> </h1>
	
 h1         = Element Node
 class      = Attribute Node
 Javascript = Text Node
 small      = Child Node
 DOM        = Text Node
	


Getting Elements

document method or window.document is used to excess any element on a webpage. These Method returns a node object or a node list( array like structure ).

document.body


 var x=document.body;
 x.nodeName		// return BODY
 x.nodeType		// return 1
 

document.images


 var y=document.images;
 y.length       // return no of images
 y[0].nodeName  // return IMG
 y[0].nodeType  // return 1 
	

DOM Shortcut Methoods

Method Use
document.body To excess body element of webpage
document.images return list of all images in document.
document.links return list of all hyperlinks in document.
document.anchors return list of only anchor tags in document.
document.forms return list of form tags in document.

nodeType

Code Type
1 element
2 attribute
3 text
8 comment
9 document

document.body is node object. But document.images is array-like object. To excess individual element inside list, use index notation. Exp, document.images[0] means first image of webpage.


Get Element By Id

document.getElementById() return the element with unique id that is given as an argument. It is supported in all major browsers.

<h1 id="heading">Javascript DOM</h1>
<script>    
 var x=document.getElementById("heading");
</script>

Get Elements By Tag Name

document.getElementsByTagName() return list of all elements with tag name given as an argument. It is supported in all major browsers.

<p> Para 1</p>
<p> Para 2</p>
<script>    
 var x=document.getElementsByTagName("p");
 x.length;			// return 2,			
 var p1=x[0];        //return first p element;
 var p2=x[1];        //return second p element;
</script>

Get Elements By Class Name

document.getElementsByClassName() return list of all elements with class name given as an argument. It is supported in all major browsers, except IE 8 and below doesn't support.

<p class="para"> Para 1</p>
<p class="para"> Para 2</p>
<script>    
 var x=document.getElementsByClassName("para");
 var p1=x[0];        //return first p element;
 var p2=x[1];        //return second p element;
</script>

Query Selector

document.querySelector() return first element in document with tag, id, or class name given as an argument. Its is pure CSS based selector. It is supported in all major browsers, except IE 7 and below. IE 8 and above support CSS 2.1, but 7 doesn't.

<p class="para" id="para1"> Para 1</p>
<p class="para" id="para2"> Para 2</p>
<script>    
 var x=document.querySelector('p');            // first p tag;
 var y=document.querySelector('#para1');       // id para1
 var z=document.querySelector('.para');        
         // first element with class para
 var a=document.querySelector('p:last-child'); // last p element 
</script>

Query Selector All

document.querySelectorAll() return list of elements in document with tag, id, or class name given as an argument. querySelectorAll() returns data in array-like structure. Its is pure CSS based selector. It is supported in all major browsers, except IE 7 and below. IE 8 and above support CSS 2.1, but 7 doesn't.

<p class="para"> Para 1</p>
<p class="para"> Para 2</p>
<p> Para 3</p>
<script>    
 var x=document.querySelectorAll('p');     // all p tags;
 var y=document.querySelectorAll('.para'); 
         // all elements with class para
 
 x.length         // return 3 
 y.length         // return 2 
 
</script>

querySelector, querySelectorAll are supported in IE 8 and above browsers. getElementsByClassName is supported in IE 9 and above browsers only.