Javascript getElementsByTagName Loop

Updated on 23 Mar 2012,
Published on 08 Jul 2008

Javascript getElementsByTagName returns a node list array collection. Javascript for loop can be used to get the value stored as HTML element at each index of the array returned by the documentElement getElementsByTagName method of Javascript. You can use the length property and item and namedItem functions of getElementsByTagName method of documentElement property of Javascript document object as we learnt in the previous article about Javascript document getElementsByTagName method. You can pass * to the getElementsByTagName method to get the array collection of all the HTML elements available in the HTML document. Here we will use the Javascript for loop through the array elements returned by the getElementsByTagName method.

JavaScript DOM Examples:

You can see the live samples and examples of JavaScript DOM from the following links:

Example of Javascript getElementsByTagName Loop

<script type="text/javascript" language="javascript">
    function getAllTags() {
        var arr = new Array();
        arr = document.getElementsByTagName("*");

        alert("Total Number of HTML Elements Found: " 
             + document.getElementsByTagName("*").length);

        for (var i = 0; i < arr.length; i++) {
            var tagName = 
                        document.getElementsByTagName("*").item(i).nodeName;
            var tagObj = 
                        document.getElementsByTagName("*").item(i);

            alert("TagName: "
                 + tagName
                 + "\n\ninnerText:\n" 
                 + tagObj.innerHTML);
        }
    }
</script>

Above example code for Javascript getElementsByTagName loop will return all the HTML tags as an array. For loop has been used to read each element at array index.

Following sample output will provide you the more advanced JavaScript code to fetch only required HTML elements by TagName:

Output:

You can see the output of above discussed code from the following link:

JavaScript document.getElementsByTagName

Continue to next tutorial: Javascript document getElementsByName Method to learn how to get the collection of HTML elements based on value of "name" attribute.

5 Responses to "Javascript getElementsByTagName Loop"
HP
Very good help is provided in this site.

Thanks
Charles Edwards
I use QtWeb, Portable Firefox and Portable IE8 in an offline fashion. I have found that, for some strange (to me anyway) reason, the getElementById() function will not work at all if it is preceded by a document.write(.. invocation; whereas it will if all document.writes are left out and only alert(.. calls are used. Can you offer any explanation for this?
Ezineasp.net
Hi Charles

Please try the same code that does not let you to invoke the getElementById() function preceded by a document.write method in IE8. It will throw an error information at the right-bottom corner of IE8's explorer bar. Double click on it and post it here.

Then we will try to find out what is wrong there.
Charles Edwards
Here is the message I get TypeError: Result of expression 'document.getElementById('abc')' [null] is not an object. I have an input block with an id of "abc" whose value I am trying to pick up. The full statement is var x = document.getElementById('abc').value; I am suspecting that it is a Javascript interpret error. This same statement without any document.write.. s works just fine. I verify this with alert.. statements. Prior to the error I have these statements: if (document.getElementById) alert("Good abc"); which work; I get the "Good abc" alert box.
Very good !! Agreed. But, this creates some issues in Google chrome. Additionally, is there a way to find the current html input element, if it's clicked?

Thanks!
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers