View Full Version : document.getElementsByTagName('*')

08-15-2002, 06:57 PM
var elements = document.getElementsByTagName('*')

What type of object is returned by this call? How do I access each item in this list? I would like to loop through the list and print each item.


08-15-2002, 07:26 PM
I figured out my own answer. I can access the items in the list like this:


08-15-2002, 07:27 PM
The getElementsByTagName() method returns a collection (an array) of all the elements in the document. You can reference it like a normal array. Here is an example:

var els=document.getElementsByTagName("*");
for(var i=0;i<els.length;i+)
document.write(els.nodeName+"<br />");

Hope that helps!

Happy coding! :)

08-15-2002, 07:42 PM
It returns a "NodeList" (defined in DOM1, DOM2, and DOM3 Core), which is basically an array with an additional "item" method where:

nodelist.item(X) == nodelist[X]

As a matter of fact, W3C never defined in their specs an array accessor to members of a NodeList, but all browsers which support DOM allow it anyway.

08-15-2002, 07:54 PM
onload = function() {
var els;
if (document.all) els = document.all;
else els = document.getElementsByTagName('*');
viewer = open('','',
for (var i=0; els[i]; ++i)
viewer.document.write(els[i].nodeName + '<br />');

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum