document.getElementById() Get element by ID
Last updated: 2026-09-09
document.getElementById() Get element by ID
getElementById gets a single element by its id attribute.
| Category | DOM |
|---|---|
| ES version | DOM |
📝 Syntax
document.getElementById('myId');
⚙️ Parameters
| id | The id of the element. |
|---|
Returns:The element or null.
Mutates the original:No (returns a new value)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
content
null
💡 Tip Returns a single element, not a collection.
ℹ️ info Returns null if not found — check before use.
🌐 Browser support
| Browser | |||||
|---|---|---|---|---|---|
| document.getElementById() | ✓ v1 | ✓ v12 | ✓ v1 | ✓ v1 | ✓ v1 |
| Supported by all modern browsers | |||||
🏷️ Related Items
💡 Related Cases
More case studies coming soon — check back later.
📚 Related Tutorials
❓ FAQ
QgetElementById vs querySelector?
AgetElementById is faster for id-only; querySelector supports any CSS selector.
QWhat if null?
ACheck with if (el) or use optional chaining.
QCan it get multiple elements?
ANo — use querySelectorAll for multiple elements.