Home JavaScript Element Properties id JavaScript Element id Property Definition Element.id gets and sets the element’s id content attribute as a string (MDN). Non-empty ids must be unique in the document.
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax Use el.id. Start with this line.
element.id / element.id = value Call What it does element.id / element.id = valuePrimary signature.
Built into the browser. Read or assign el.id — no import required.
Library declarations element.id / element.id = value Parameters Parameter Description getElementById Optional. Lookup by that same string
Return Value Return Value Description * The current value of el.id.
JavaScript Examples 1. Read id 2. Set id 3. getElementById Example 1 — Read id Read the identifier from an element that already has one.
const hero = document.getElementById("hero");
console.log(hero.id);
// "hero"
console.log(hero.getAttribute("id"));
// "hero" (same value via the attribute) How It Works Find the node. getElementById("hero") returns the element.Read .id. The property returns the string "hero".Attribute match. getAttribute("id") shows the same reflected value.Missing id? You would get "", not null.Example 2 — Set id Assign a new identifier; the HTML attribute updates with it.
const box = document.querySelector("div");
box.id = "panel";
console.log(box.id); // "panel"
console.log(box.getAttribute("id")); // "panel" How It Works Select a target. Any element can receive an id.Assign. box.id = "panel" writes the identifier.Reflection. The id attribute on the element updates too.Keep it unique. Do not reuse "panel" on another node in the same document.Example 3 — getElementById + Case Lookup uses the exact string — case must match.
const el = document.createElement("div");
el.id = "Hero";
document.body.appendChild(el);
console.log(document.getElementById("Hero") === el); // true
console.log(document.getElementById("hero")); // null How It Works Set a cased id. "Hero" is stored exactly as written.Exact match works. getElementById("Hero") returns the node.Wrong case fails. "hero" is a different string → null.MDN tip. Avoid ids that differ only by capitalization.Compatibility
Browser Support Element.id is Baseline Widely available (MDN: since July 2015). Safe across modern and older evergreen browsers.
✓ Baseline · Widely available
Element.id Get and set the element's id attribute string — unique when non-empty.
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support id Excellent
Bottom line: Safe for production. Prefer unique, case-consistent ids and look them up with getElementById.
Frequently Asked Questions What does element.id do? It gets and sets the element's identifier string, reflecting the HTML id attribute.
Is Element.id deprecated or experimental? No. MDN marks Element.id as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Must an id be unique? Yes. If the id is not the empty string, it must be unique in the document so getElementById and CSS #id selectors work correctly.
Are ids case-sensitive? Yes. Identifiers are case-sensitive. Avoid IDs that differ only by capitalization.
How does id relate to getElementById? document.getElementById("hero") finds the element whose id is "hero".
What if an element has no id? Reading el.id returns "". Setting el.id = "panel" adds or updates the id attribute.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion