Home JavaScript Document Methods getElementById() JavaScript Document getElementById() Method Definition document.getElementById(id) returns the Element whose id matches the given string, or null if none is found. IDs should be unique, so this is a fast way to grab one specific node (MDN).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax Call document.getElementById. Start with this line.
document.getElementById(id) Call What it does document.getElementById(id)Primary signature. document.getElementById(...)Call with the arguments your case needs.
Built into the browser. Call getElementById(...) on document — no import required.
Library declarations document.getElementById(id) Parameters Parameter Description id Required. Case-sensitive string matching the element's id
Return Value Return Value Description * Element or null
JavaScript Examples 1. Find by id 2. Handle null 3. Case sensitivity Example 1 — Find an Element by Id Look up a paragraph and read its text.
// HTML: <p id="para">Hello DOM</p>
const elem = document.getElementById("para");
console.log(elem !== null); // true
console.log(elem.textContent); // "Hello DOM"
console.log(elem.id); // "para" How It Works Match the id. The string must equal the element’s id.Get an Element. Success returns the node object, not a string.Read properties. Use textContent, id, and so on.Keep ids unique. One id → one clear target.Example 2 — Missing Id Returns null Always guard before you touch the result.
const missing = document.getElementById("no-such-id");
console.log(missing); // null
if (missing) {
console.log(missing.textContent);
} else {
console.log("not found");
} How It Works Lookup fails. No matching id → null.Do not chain blindly. null.textContent throws.Branch on null. An if keeps the code safe.Fix the cause. Typo, wrong case, or element not in the document yet.Example 3 — Case-Sensitive Ids Same letters, different case → different lookup.
// HTML: <div id="Main">Box</div>
const ok = document.getElementById("Main");
const bad = document.getElementById("main");
console.log(ok !== null); // true
console.log(bad); // null
// Flexible alternative when you need a selector:
const viaQs = document.querySelector("#Main");
console.log(viaQs === ok); // true How It Works Exact match. "Main" ≠ "main" for this API.Wrong case → null. Easy source of silent bugs.Copy the id carefully. Match the HTML attribute exactly.Need selectors? Use querySelector("#Main") for CSS-style lookups.Compatibility
Browser Support Document.getElementById() is a long-standing DOM lookup API. MDN lists it as Baseline Widely available (since July 2015).
✓ Baseline · DOM
Document.getElementById() Supported everywhere modern JavaScript runs. Safe as the classic way to select a unique element by id.
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 getElementById() Excellent
Bottom line: Safe and ubiquitous. Prefer it for unique ids; use querySelector for other selectors. See the MDN getElementById() reference .
Frequently Asked Questions What does document.getElementById() do? It returns an Element whose id matches the specified string, or null if no matching element is found. IDs should be unique in a document.
Is getElementById() deprecated or experimental? No. MDN marks Document.getElementById() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
What if the id is missing? The method returns null. Always check for null before reading properties like textContent or style.
Is the id case-sensitive? Yes. The id string is case-sensitive. document.getElementById("Main") will not find an element with id="main".
Why does getElementByID (capital D) fail? Capitalization of "Id" must be correct. getElementByID() is not valid and will not work.
When should I use querySelector instead? 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.
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.
11 people found this page helpful
Helpful Share Copy link Suggestion