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
    Apr 2006
    Location
    North Carolina
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Syntax to loop through document using getElementsByTagName - Need direction - Urgent

    I have been working on this for a couple of days and I am not getting anywhere. I need to loop through the document, grab particular tags and create a list of the ids in these particular tags. I need to get the id for only the tags that have an id that starts with "pos".

    For example, the tags I want to pull out look like:

    <div id="pos-RCH" title="RCH"></div>
    <div id="pos-SKY" title="SKY"></div>
    <div id="pos-LDB" title="LDB"></div>
    <div id="pos-PRO" title="PRO"></div>

    I need to create a list that would look like: pos-RCH, pos-SKY, pos-LDB, pos-PRO.

    I need to make this call as generic as possible because other people will be coding different ids but will have to follow the naming convention of "pos-" for the ids in the div tags.

    I am assuming I need to do something like this:

    var alldivs = document.getElementsByTagName("div");

    for (var i = 0; i < alldivs.length; i++) {
    //do something to create this list (pos-RCH, pos-SKY, pos-LDB, pos-PRO)
    }

    Please, if anyone can direct me, I would truly appreciate it.

  • #2
    Regular Coder fishluvr's Avatar
    Join Date
    Nov 2005
    Posts
    110
    Thanks
    1
    Thanked 12 Times in 12 Posts
    Something like:
    PHP Code:
    <script type="text/javascript">
    var 
    posArray = new Array();
    var 
    alldivs document.getElementsByTagName("div");
    function 
    getIds(){
      for (var 
    0alldivs.lengthi++) {
        if (
    alldivs[i].getAttribute('id') != null && alldivs[i].getAttribute('id').match('pos-')) {
          
    posArray[posArray.length] = alldivs[i].getAttribute('id');
        }
      }
      
    alert(posArray); // Just to show the results.
    }
    </script> 

  • #3
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd use the RegExp test() method instead.

    Code:
    var alldivs = document.getElementsByTagName("div");
    var re = new RegExp('^pos-','i');
    
    for (var i = 0; i < alldivs.length; i++) {
      if(alldivs[i].id && re.test(alldivs[i].id)) // add it to the list
      ....
    }
    More Information.
    Last edited by Kravvitz; 04-14-2006 at 11:20 PM.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #4
    New to the CF scene
    Join Date
    Apr 2006
    Location
    North Carolina
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! I was able to get it to work by using both methods. Thanks so much for your help!

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may do it generic, by using a wildcard to grab all the taged elements

    var alltags = document.getElementsByTagName('*')
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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