SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing the ID of a tag?

    Hi, How do I access a tag's ID with the GetElmentBy method?

    Shouldn't this: "document.getElementsByTagName('div').id;"

    work?

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    <div id="example"></div>

    document.getElementById('example');

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    getElementsByTagName() always returns a NodeList, so you need to specify which node in the NodeList you want to deal with.

    For example, you can do this:
    Code:
    alert(document.getElementsByTagName('div')[0].id);
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  4. #4
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Kravitz thanks a lot for your reply, I didn't realize that it would always return a Nodelist but I guess it would make sense! Very handy to know that!

    Cheers


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
  •