Quiz Entry - updated: 2026.06.20
How do you select a single element by its ID in JavaScript?
Call document.getElementById("id") (no #), which returns the matching element or null if none exists.
const element = document.getElementById("myButton");
Key characteristics:
- Returns a single element (IDs must be unique)
- Returns
nullif no element matches - Fastest selection method (IDs are indexed)
- Does NOT include the
#symbol (unlike CSS)
Common mistake:
// Wrong - don't use #
document.getElementById("#myButton");
// Correct
document.getElementById("myButton");