Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    document.getElementsByTagName('*')

    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.

    Thanks!

  • #2
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I figured out my own answer. I can access the items in the list like this:

    elements.item(i).tagName

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    var els=document.getElementsByTagName("*");
    for(var i=0;i<els.length;i+)
    document.write(els.nodeName+"<br />");
    Hope that helps!

    Happy coding!

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    onload = function() {
    var els;
    if (document.all) els = document.all;
    else els = document.getElementsByTagName('*');
    viewer = open('','',
    'width=120,height=500,left=100,top=10,scrollbars');
    for (var i=0; els[i]; ++i)
    viewer.document.write(els[i].nodeName + '<br />');
    viewer.document.close();
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •