SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: Accessing the ID of a tag?
-
Jun 13, 2007, 10:49 #1
- 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?
-
Jun 13, 2007, 12:01 #2
- 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');
-
Jun 13, 2007, 14:08 #3
- 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.
-
Jun 14, 2007, 02:54 #4
- 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