document.getElementById() is an instance method that returns the Element whose id matches a string (see MDN Document: getElementById()). Learn the id parameter, null when missing, case sensitivity, why spelling is Id (not ID), how it compares to querySelector, and five try-it labs.
01
Kind
Instance method
02
Arg
id (string)
03
Returns
Element | null
04
IDs
must be unique
05
Case
sensitive
06
Status
Baseline
Fundamentals
Introduction
Almost every beginner DOM demo starts here: give an element an id, then grab it with document.getElementById("..."). Because IDs should be unique in a document, this is a fast, clear way to reach one specific node.
MDN: the method returns an Element whose id property matches the specified string. If you need an element without an ID, use querySelector() with any CSS selector instead.
💡
Think: look up by name badge
1) Put id="para" on an element in HTML 2) Call document.getElementById("para") 3) Get the element — or null if missing 4) Update text, style, or listeners
getElementById searches the document tree. Keep a local variable reference after createElement, or append before looking up by id.
Example 5 — Same node via querySelector("#id")
Both can find an id; getElementById stays the classic path.
JavaScript
const byId = document.getElementById("hero");
const bySel = document.querySelector("#hero");
console.log(byId === bySel); // true when both find the same node
Document.getElementById() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.getElementById()
The classic DOM lookup by unique id — Element or null across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
getElementById()Wide
Bottom line: Use getElementById for unique ids. Prefer querySelector when you need classes, attributes, or complex selectors.
Wrap Up
Conclusion
document.getElementById(id) is the fastest mental model for grabbing one unique node: pass the id, get an Element or null. Keep ids unique, match case exactly, spell the method Id, and fall back to querySelector when you do not have an id.
Prefer querySelector when selecting by class or structure (MDN)
❌ Don’t
Write getElementByID (capital D) — it will not work (MDN)
Assume a fresh createElement is findable before insert (MDN)
Call getElementById on a parent element (not available) (MDN)
Reuse the same id on multiple nodes
Skip null checks in production UI code
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getElementById()
Find one unique element by id — or get null.
5
Core concepts
📝01
Returns
Element | null
MDN
🔄02
Case
sensitive
exact
✍️03
Spelling
Id not ID
MDN
⚡04
Alt
querySelector
no id
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.getElementById() returns an Element whose id property matches the specified string, or null if no matching element is found. IDs should be unique in a document.
No. MDN marks Document.getElementById() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The method returns null (MDN). Always check for null before reading properties like textContent or style.
Yes. MDN: the id string is case-sensitive. document.getElementById("Main") will not find an element with id="main".
MDN: capitalization of "Id" must be correct. getElementByID() is not valid and will not work.
MDN: if the element has no id, use querySelector() with any CSS selector. getElementById() is still the classic, fast path when you have a unique id.
Did you know?
MDN’s usage notes show why there is no parent.getElementById(): ids must be unique in the whole document, so a “local” search would not make sense the way class or tag lookups do.