Javascript getElementsByTabName 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.
<head>
<title>Javascript Get All Tags</title>
<script type="text/javascript" language="javascript">
{
}
</script>
</head>
<div id="div1">
First HTML div tag
<div id="div2">
Second HTML div tag
<p id="p1">
HTML paragraph p tag
<div id="div3">
Third HTML div tag
</html>
Above example code for Javascript getElementsByTagName loop will return all the HTML tags as an array. For loop is used to read each element at array index. Copy and paste the above code to create an HTML web page and run it in a web browser, click on the button, it will display the alert message box for the each HTML element matched in the switch case to display its innerText and value of id attribute.
Be the first to rate this post
Tags: javascript, html, dom, javascript document getelementsbytagname, javascript for loop, html div tag, html span tag, html p tag, javascript getelementsbytagname all, javascript documentelement, javascript document object, javascript getelementsbytagname loop, javascript loop through array
8/9/2008 5:55:50 PM
Very good help is provided in this site. Thanks
HP
8/23/2008 8:41:37 PM