Element.id

Summary

Gets or sets the element's identifier (attribute id).

Syntax

var idStr = element.id; // Get the id.
element.id = idStr; // Set the id

where idStr is the ID of the element.

Notes

The ID must be unique in a document, and is often used to retrieve the element using getElementById.

In some documents (in particular, HTML, XUL, and SVG), the id of an element can be specified as an attribute on the element like so: <div id="main-content">.

However you can't use this attribute in a custom XML document without correctly specifying the type of the id attribute in the DOCTYPE.

Other common usages of id include using the element's ID as a selector when styling the document with CSS.

Note that IDs are case-sensitive, but you should not create IDs that differ only in the capitalization (see Case Sensitivity in class and id Names).

Specification

Specification Status Comment
Document Object Model (DOM) Level 2 HTML Specification Recommendation  

 

Document Tags and Contributors

Last updated by: kscarfone,