get an element's id

Rana picture Rana · Sep 2, 2010 · Viewed 441.4k times · Source

Is there another way to get an DOM element's ID?

element.getAttribute('id')

Answer

Nick Craver picture Nick Craver · Sep 2, 2010

Yes you can just use the .id property of the dom element, for example:

myDOMElement.id

Or, something like this:

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
  alert(inputs[i].id);
}