开发者

accessing id property via javascript

开发者 https://www.devze.com 2022-12-31 17:08 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号