Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Firefox Document.all equivilant

Status
Not open for further replies.

RicksAtWork

Programmer
Nov 1, 2005
120
GB
Firefox Document.all equivilant....

I use getElementById when I know the id, however I need to iterate through all the elements on the page i.e.

for (i=0; i<document.all.length;i++)
{
}

However document.all is proprietry to IE...

How can I perform this same functionality in a W3C standard DOM way??
 
What exactly do you mean by all the elements on the page?

cLFlaVA's method above will expose all the elements within the body tag. If you want to expose other things like embeded styles, code tags, etc, there are other methods for those types.

With cLFlaVA's method above you can test the page elements to see if they have a name or ID attribute like this:
Code:
  var c = document.getElementsByTagName("body")[0].getElementsByTagName("*");
  var outstr="";
  for (var x=0;x<c.length;x++) {
    outstr=outstr+"tagName: "+c[x].tagName+", ";
    if (c[x].id)
      outstr=outstr+"ID: "+c[x].id+", ";
    if (c[x].name)
      outstr=outstr+"Name: "+c[x].name+", ";
    if (c[x].type)
      outstr=outstr+"Type: "+c[x].type+", ";
    outstr=outstr+'\r\n';
    }
  alert(outstr);

I just build a string to output in an alert to show all the elements and a few of their attributes. If it has an id, name or type then it is added to the string.
You can just as easily use each test to know the ID or name of the item being accessed though at this point you already have the object reference and can make changes to it directly.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top