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 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2002
    Location
    Wichita, KS
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Traversing the 'getElementsByTagName' collection.

    Ok simply question, hopefully, simple answer.

    How would I go about using the document.getElementsByTagName in a reference sense.

    ie: placing a specific collection type into an array, so I can actually see the id's and such of all the referenced items (such as DIV's).

    So that if I, for instance, use the following make believe function name "getAll('div')" it'll return an alert with the follow information

    You searched for the following 'tag-type' of "DIV". These are the only elements with the DIV tag:

    divId1, divID2, divID3, divID4,etc.,etc.,etc.

    Hopefully you can see where I'm going with this. Main reason for this type of script is for debugging (say for misspelled div names and I can't find out where they are). Any help would be appreciated


    Leithakor
    James
    Leithakor vanwa ilya eile, lat vanwa leithakor.
    Translatation: Freedom over all else, Honor over Freedom

  • #2
    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
    var divs = document.getElementsByTagName('div');
    var message = 'There are ' + divs.length + '<div>\'s in this page with the following ids:\n';
    for (var i = 0; i < divs.length; i++)
    message += divs.item(i).getAttribute('id') + '\n';

    alert(message);

  • #3
    New Coder
    Join Date
    Dec 2002
    Location
    Wichita, KS
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excellent. Thank you very much =) Not certain why I didn't think about that. Very much appreciated.


    Leithakor
    James
    Leithakor vanwa ilya eile, lat vanwa leithakor.
    Translatation: Freedom over all else, Honor over Freedom


  •  

    Posting Permissions

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