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
2010-10-10 07:19:30
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
2010-10-10 07:26:27
neither it gave me id of body element.
Rahul Utb
2010-10-10 07:27:32
@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
2010-10-10 07:28:04
why then var id = anyElement.id, not giving id of my page's BODY attribute
Rahul Utb
2010-10-10 07:31:43
? You never said anything about getting the body (`document.body.id`) or the first div in the page (`document.getElementsByTagName('div')[0].id`).
bobince
2010-10-10 07:36:08
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
2010-10-10 07:42:27
+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
2010-10-10 07:36:39
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
2010-10-10 07:42:45
@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
2010-10-10 07:48:54