tags:

views:

59

answers:

2

Is there any way of determining the id attribute of any html element.

+4  A: 

It's simple as pie:

var id = anyElement.id

The reverse function (determine element by id):

var anyElement = document.getElementById(id);
Lekensteyn
sorry its not at all solving my problem. Actually I want to determing the id of FIRST DIV element in my page. This div is just after body.
Rahul Utb
neither it gave me id of body element.
Rahul Utb
@Rahul - Then a (-1) for your question. You should be explicit from the beginning with what you want and not waste people's time.
Dan Dumitru
why then var id = anyElement.id, not giving id of my page's BODY attribute
Rahul Utb
? You never said anything about getting the body (`document.body.id`) or the first div in the page (`document.getElementsByTagName('div')[0].id`).
bobince
ya I tried this code but this too not giving me the id, '_firebugConsole' is the msg I get. FYI I have firebug installed in ff.
Rahul Utb
+3  A: 

I suggest you edit your question to represent accurately what you are looking for.

Based on your comments, if you are looking for the FIRST DIV, use something like this:-

x=document.getElementsByTagName("div"); 
if (x!= 'undefined' && x.length > 0 ) 
   document.write(x[0].id);
InSane
ya I tried this code but this too not giving me the id, '_firebugConsole' is the msg I get. FYI I have firebug installed in ff.
Rahul Utb
@Rahul Utb - can you view your HTML Source and search for _firebugConsole and see if there is actually a div of that name? I am not sure how firebug works internally...
InSane