JavaScript Element id Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance property

What You’ll Learn

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

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.

JavaScript
const hero = document.getElementById("hero");
console.log(hero.id); // "hero"
hero.id = "banner";
💡
Beginner tip

The HTML attribute and the DOM property share the same name: id. Changing el.id updates the attribute in the live document.

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).

📝 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.

ItemDetail
Typestring
AccessGet and set
HTML attributeid
Common lookupdocument.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.

📋 MDN Identifier Shape

MDN highlights that id reflects the global attribute and is often used with getElementById and CSS selectors:

JavaScript
<section id="hero">Welcome</section>
JavaScript
const hero = document.getElementById("hero");
console.log(hero.id); // "hero"
hero.id = "banner";
console.log(document.getElementById("banner") === hero); // true

Related learning: className, getAttribute("id"), and setAttribute("id", ...).

⚡ Quick Reference

GoalCode / note
Readel.id
Writeel.id = "panel"
Clearel.id = ""
Find by iddocument.getElementById("panel")
CSS selector#panel / document.querySelector("#panel")
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.id.

Kind
get / set

Instance

Type
string

Identifier

Attribute
id

HTML name

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: id. Labs use simple elements so you can read, set, and look up identifiers safely.

📚 Getting Started

Read and update the identifier like everyday DOM work.

Example 1 — Read id

Log the identifier that matches the HTML id attribute.

JavaScript
const hero = document.getElementById("hero");
console.log(hero.id);
console.log(hero.getAttribute("id"));
Try It Yourself

How It Works

el.id and getAttribute("id") both reflect the same identifier for normal HTML elements.

📈 Set, Lookup & Edge Cases

Change an id, find it again, and see empty-string / case behavior.

Example 2 — Set id

Assigning id updates the live attribute and future lookups.

JavaScript
const box = document.querySelector("div");
box.id = "panel";
console.log(box.id);
console.log(box.outerHTML.includes('id="panel"'));
Try It Yourself

How It Works

Writing el.id is the same idea as setAttribute("id", ...) for HTML elements—the DOM and the markup attribute stay aligned.

Example 3 — getElementById

After renaming, the old id no longer finds the element; the new one does.

JavaScript
const el = document.getElementById("hero");
el.id = "banner";
console.log(document.getElementById("hero")); // null
console.log(document.getElementById("banner") === el); // true
Try It Yourself

How It Works

getElementById always looks up the current identifier. Renaming an element invalidates the previous id string.

Example 4 — Empty String & Case

No id yields "". IDs are case-sensitive.

JavaScript
const plain = document.querySelector("p");
console.log(plain.id); // ""
plain.id = "Note";
console.log(document.getElementById("note")); // null (different case)
console.log(document.getElementById("Note") === plain); // true
Try It Yourself

How It Works

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)"
});
Try It Yourself

How It Works

Safe to use in all modern browsers and IE. Prefer meaningful, unique, case-consistent IDs.

🚀 Common Use Cases

  • Finding one element quickly with document.getElementById().
  • Hooking CSS rules with #id selectors.
  • Linking labels and controls (<label for="email">).
  • Assigning a temporary id in JS for a script that expects one.
  • Clearing an id (el.id = "") when uniqueness must move elsewhere.

🔧 How It Works

1

Element has an id attribute

Or none—then the property reads as "".

DOM
2

Read or write el.id

Getter returns the string; setter updates the attribute.

Reflect
3

Lookups use the current value

getElementById and #id CSS follow the live id.

Lookup
4

Keep non-empty ids unique

Case-sensitive strings—one id, one element.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Non-empty ids must be unique in the document.
  • Identifiers are case-sensitive; avoid near-duplicates that differ only by case.
  • Related: firstElementChild, className, innerHTML, JavaScript hub.

Browser Support

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).

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported
Yes
id Baseline

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.

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.

Continue with innerHTML, className, firstElementChild, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Keep non-empty ids unique in the document
  • Use clear, meaningful names (hero, nav-main)
  • Prefer a consistent case style (often lowercase)
  • Use getElementById for a single known id
  • Update lookups after you rename an element’s id

❌ Don’t

  • Reuse the same id on multiple elements
  • Create IDs that differ only by capitalization
  • Start ids with a digit (invalid for CSS #id in many cases)
  • Overuse ids when a class would scale better
  • Assume old id strings still work after a rename

Key Takeaways

Knowledge Unlocked

Five things to remember about id

Get/set string that reflects the HTML id attribute.

5
Core concepts
📝 02

string

or empty

Type
🔍 03

Lookups

getElementById

Use
04

Baseline

widely available

Status
🎯 05

Unique

when non-empty

Tip

❓ Frequently Asked Questions

It gets and sets the element's identifier, reflecting the id HTML attribute as a string.
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 within the document so getElementById and CSS #id selectors work correctly.
Yes. Identifiers are case-sensitive. Avoid creating IDs that differ only by capitalization.
document.getElementById("hero") finds the element whose id property (and id attribute) is "hero".
Reading el.id returns an empty string (""). Setting el.id = "panel" adds or updates the id attribute.
Did you know?

Fragment links like https://example.com/page#hero scroll to the element whose id is hero—the same string Element.id exposes.

Next: innerHTML

Learn how Element.innerHTML gets and sets HTML inside an element.

innerHTML →

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.

5 people found this page helpful