JavaScript Element id Property

Beginner
5 min read
Updated: Nov 2026
3 examples
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.

JavaScript
element.id / element.id = value
CallWhat it does
element.id / element.id = valuePrimary signature.

Built into the browser. Read or assign el.id — no import required.

Library declarations
JavaScript
element.id / element.id = value

Parameters

ParameterDescription
getElementByIdOptional. Lookup by that same string

Return Value

Return ValueDescription
*The current value of el.id.

JavaScript Examples

Example 1 — Read id

Read the identifier from an element that already has one.

JavaScript
const hero = document.getElementById("hero");

console.log(hero.id);
// "hero"

console.log(hero.getAttribute("id"));
// "hero"  (same value via the attribute)
Try It Yourself

How It Works

  1. Find the node. getElementById("hero") returns the element.
  2. Read .id. The property returns the string "hero".
  3. Attribute match. getAttribute("id") shows the same reflected value.
  4. Missing id? You would get "", not null.

Example 2 — Set id

Assign a new identifier; the HTML attribute updates with it.

JavaScript
const box = document.querySelector("div");

box.id = "panel";
console.log(box.id);                 // "panel"
console.log(box.getAttribute("id")); // "panel"
Try It Yourself

How It Works

  1. Select a target. Any element can receive an id.
  2. Assign. box.id = "panel" writes the identifier.
  3. Reflection. The id attribute on the element updates too.
  4. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Set a cased id. "Hero" is stored exactly as written.
  2. Exact match works. getElementById("Hero") returns the node.
  3. Wrong case fails. "hero" is a different string → null.
  4. MDN tip. Avoid ids that differ only by capitalization.

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.

99% Widely available
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

It gets and sets the element's identifier string, reflecting the HTML id attribute.
No. MDN marks Element.id as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Yes. If the id is not the empty string, it must be unique in the document so getElementById and CSS #id selectors work correctly.
Yes. Identifiers are case-sensitive. Avoid IDs that differ only by capitalization.
document.getElementById("hero") finds the element whose id is "hero".
Reading el.id returns "". Setting el.id = "panel" adds or updates the id attribute.

Next: innerHTML

Learn how to get and set an element’s HTML content as a string.

innerHTML tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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