Dennis D
Dennis D

Reputation: 1343

accessing id property via javascript

Is there a way to access the ID of a DOM element?

I don't mean using the getElementById 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.

Upvotes: 2

Views: 1420

Answers (4)

Andrew Childs
Andrew Childs

Reputation: 2985

I've verified that both element.id and element.getAttribute('id') work in Chrome/Safari/FF latest and IE 6-8.

Upvotes: 1

Juan de Dios
Juan de Dios

Reputation: 2789

It's works.

element.getAttribute('id');

Upvotes: 1

Pointy
Pointy

Reputation: 413702

In jQuery it's

 $(this).attr('id')

or

 $(this)[0].id

Upvotes: 3

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195972

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.

Upvotes: 3

Related Questions