SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Addict SLEE's Avatar
    Join Date
    Aug 2003
    Location
    uk
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get names of id's on a page

    can someone help me on how to get teh names of the id's on a page and display them. im a bit stuck i think it will need to use regular expression but im not up on regular expression. should i use getelementbytag frst to get the divs on the page then somehow get the name from that of each id="" part/

    many thanks
    Last edited by SLEE; Mar 19, 2007 at 16:31.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you only want it for divs:
    Code:
    var divs = document.getElementsByTagName('div');
    var ids = '';
    for (var i=0; i<divs.length; i++) {
      if (divs[i].hasAttribute('id')) {
        ids += divs[i].getAttribute('id') + ', ';
      }
    }
    var p = document.createElement('p');
    p.appendChild(document.createTextNode(ids));
    document.getElementsByTagName('body')[0].appendChild(p);
    Assumes you want a comma separated list of them to appear at the end of your page.

  3. #3
    SitePoint Addict SLEE's Avatar
    Join Date
    Aug 2003
    Location
    uk
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excellent thanks but i want to do it to class aswell but i cat get that to work?

    really appreciate it i didnt even know there was a hasAttribute thats really helpful

    i was using this before you showed me:

    var aDivs = document.body.getElementsByTagName("DIV");
    for (x in aDivs)
    {
    if(aDivs[x].hasAttribute('id')){
    document.write(aDivs[x].id + "<br />")
    }

    if(aDivs[x].hasAttribute('class')){
    document.write(aDivs[x].class + "<br />")
    }
    }

    and it wouldnt allow for class for some reason
    Last edited by SLEE; Mar 19, 2007 at 17:49. Reason: got it working

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's because you have to use getAttribute('class') or aDivs[x].className (not .class).

  5. #5
    SitePoint Addict SLEE's Avatar
    Join Date
    Aug 2003
    Location
    uk
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah i see, simple when you know how

    why is it in my way teh browser seems to carry on doing something?
    is there a way using your code to put each one on a new line?

    thanks again for the help

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var divs = document.getElementsByTagName('div');
    var p = document.createElement('p');
    for (var i=0; i<divs.length; i++) {
      if (divs[i].hasAttribute('id')) {
        p.appendChild(document.createTextNode(divs[i].getAttribute('id')));
        p.appendChild(document.createElement('br'));
      }
    }
    document.getElementsByTagName('body')[0].appendChild(p);

  7. #7
    SitePoint Addict SLEE's Avatar
    Join Date
    Aug 2003
    Location
    uk
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah i see thanks for that the only problem i have now is thst this wont work in internet explorer. it doesnt like the hasAttribute() but i tried changing it to divs[i].id !== undefined and it still only works in firefox. ie writes out null to the screen. again i really appreciate the help

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, I thought this was just for debugging or something of the sort. I'm not sure about this, but try divs[i].id != 'undefined' or !divs[i].id or divs[i].id != ''. I'm not sure how it should be, can't test it at the moment. You might also want to try using getAttribute and seeing if it's undefined/an empty string.


Bookmarks

Posting Permissions

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