Accessing an element in document

Every HTML document is actually present in layers. According to the hierarchy, we can reach any of them as per our use. Beginning with some concepts, we will eventually move to a simple example to illustrate those points.

Till now you must have been familiar to the most powerfull tool of Javascript
It helps us to access any particular ID present on the page directly , without any reference to its parents or any other relatives. However, in many situations if we wanna to access a div which is annonymous or through some automated code, we need to use other hierarchy relationships.

One of the most powerfull and broad way of collecting all the tags present on the page is :-
Tag refers to everything on the page that starts with a "<>" and ends with a "" . Ex - html, body, a , img etc.
So, this method returns with an array of all the tags present on the page together. Often using a filter is advisable depending upon the use.

Suppose, we wanna impose some condition on the links on our website. For this we need to collect all the elements on our page with a(link) tag.
document.getElementByTagName('<a>') As a result we have a list of all the links on our document . Now, we can impose any condition on them as per as our wish.

Lets begin HTML DOM traversing with a simple example

           <ol id='ol1'>
                <li id='li1'>
                       <span id='span1'>
                             This is Text 1
                  <li id='li2'>
                        <span id='span2'>
                              This is Text 2
                 <li id='li3'>
                        <span id='span3'>
                              This is Text 3

Suppose we wanna refer to the id ol1 in the above example, it can be done in a number of ways :-



Basically DOM traversing can be very useful both for the user and admin of a website. User can use this to access data from some other site by looking into the source code of the website, while, the admin of the website can use it to update or place contents to any particular page of the website. Infact, Ajax uses this to place the updated contents at correct place.
For an example, suppose we wanna read the contents of span3 in above example . It can be done by :-


We will discuss about node properties and methods in next chapter.

For some more methods of DOM , lets move forward :-

Member Login

Member Login

Not a Member? Sign Up!

Login to comment

Be the first to comment on this topic


<<< Wanna review

Continue >>>