Is there a way to access the ID of a DOM element?
I don't mean using the g开发者_开发知识库etElementById method to get an array of objects.
Basically I already know the DOM element and that object reference is at hand.
All I need is to access the ID property.
I know something like
if(element.id==value)
won't work.
Thanks in advance.
Your code should work if the element
variable holds the dom element reference and it has an id defined..
Keep in mind that getElementById
will not return an array of object but a single object. You might be confusing it with getElementsByTagName
.
In jQuery it's
$(this).attr('id')
or
$(this)[0].id
It's works.
element.getAttribute('id');
I've verified that both element.id
and element.getAttribute('id')
work in Chrome/Safari/FF latest and IE 6-8.
精彩评论