Javascript getElementsByTagName Loop

by top54u.com 03 Jul, 2008
Spotlight.....

Javascript getElementsByTagName Loop 

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.

 

Example of Javascript getElementsByTagName Loop

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >

<head>

<title>Javascript Get All Tags</title>

 

<script type="text/javascript" language="javascript">

function getAllTags()

{

var arr = new Array(); arr = document.documentElement.getElementsByTagName("*");

 

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

 

for(var i=0; i < arr.length; i++)

{

var tagName = document.documentElement.getElementsByTagName("*").item(i).nodeName; switch (tagName)

{

case "DIV": var tagObj = document.documentElement.getElementsByTagName("*").item(i);

 

alert("TagName: " + tagName + "\n\ninnerText:\n" + tagObj.innerText); break;

 

 

case "SPAN": var tagObj = document.documentElement.getElementsByTagName("*").item(i);

 

alert("TagName: " + tagName + "\n\ninnerText:\n" + tagObj.innerText); break;

 

 

case "P": var tagObj = document.documentElement.getElementsByTagName("*").item(i);

 

alert("TagName: " + tagName + "\n\ninnerText:\n" + tagObj.innerText); break;

 

 

default:

 

}

}

}

 

</script>

</head>

<body>

<div id="div1">

First HTML div tag

</div>

 

<div id="div2">

Second HTML div tag

</div>

 

<p id="p1">

HTML paragraph p tag

</p>

 

<span id="span1">HTML span tag</span>

 

<br />

 

<div id="div3">

Third HTML div tag

</div>

 

<br />

 

<input id="btn1" type="button" value="get all tag" onclick="getAllTags();" />

 

</body>

</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.

Comments

8/9/2008 5:55:50 PM

HP

Very good help is provided in this site.

Thanks

HP in

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/23/2008 8:41:37 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....