Element.id is a read/write instance property that reflects the element’s id attribute. Learn how to read and set it, keep IDs unique, use them with getElementById and CSS—with five examples and try-it labs.
01
Kind
Instance property
02
Access
Get and set
03
Type
string
04
Attribute
id
05
Status
Baseline widely
06
Rule
Unique in document
Fundamentals
Introduction
Almost every beginner tutorial starts with id="something" in HTML. In JavaScript, that same value is available as element.id—a simple string you can read or change.
IDs are the fastest way to find one element with document.getElementById(), and they power CSS selectors like #hero. If the value is not empty, it must be unique in the document.
The HTML attribute and the DOM property share the same name: id. Changing el.id updates the attribute in the live document.
Concept
Understanding the Property
MDN: the id property of the Element interface represents the element’s identifier, reflecting the id global attribute.
Get / set — readable and writable string.
Reflects id — stays in sync with the HTML attribute.
Unique when non-empty — required for reliable lookups and CSS.
Baseline — widely available since July 2015 (MDN).
Foundation
📝 Syntax
JavaScript
id
Value
A string. Empty string means no identifier. Non-empty values should be unique in the document. IDs are case-sensitive—avoid pairs that differ only by capitalization.
Item
Detail
Type
string
Access
Get and set
HTML attribute
id
Common lookup
document.getElementById(id)
⚠️
Uniqueness
Duplicate IDs break expectations: getElementById returns only one match, and CSS #id rules can behave unpredictably. Keep each non-empty id unique.
Pattern
📋 MDN Identifier Shape
MDN highlights that id reflects the global attribute and is often used with getElementById and CSS selectors:
MDN notes identifiers are case-sensitive. Prefer consistent lowercase (or a clear naming style) and never rely on near-duplicates that differ only by case.
Example 5 — Support Snapshot
Feature-detect and remember uniqueness + case tips.
JavaScript
console.log({
supported: "id" in Element.prototype,
returns: "string",
tip: "Keep non-empty ids unique; they are case-sensitive",
status: "Baseline Widely available (MDN)"
});
Element.id is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.id
Get/set — reflects the id attribute as a string (unique when non-empty).
BaselineWidely available
Google ChromeSupported
Yes
Microsoft EdgeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported
Yes
idBaseline
Bottom line: Use Element.id to read or change an element's identifier. Keep non-empty ids unique and case-consistent so getElementById and CSS #id selectors stay reliable.
Wrap Up
Conclusion
Element.id is the DOM mirror of the HTML id attribute—a string you can read and write. Use it with getElementById and CSS, keep non-empty values unique, and treat IDs as case-sensitive.