HTML DOM getElementById() method
Example
Return the element with the specified ID:
document
.
getElementById
(
"
demo
"
)
;
Try it »
Definition and Usage
The getElementById() method returns a reference to the first object with the specified ID.
HTML DOM defines multiple methods for finding elements, besides getElementById(), there are also getElementsByName() and getElementsByTagName().
If no element with the specified ID is found, return null
If there are multiple elements with the specified ID, return the first one.
If you need to find elements that do not have an ID, you can consider using querySelector().
Browser support





All major browsers support the getElementById() method
Syntax
document.getElementById(
elementID
)
Parameters
| Parameters | Type | Description |
|---|---|---|
| elementID | String | Required. The value of the element's ID attribute. |
Return Value
| Type | Description |
|---|---|
| Element object | The element with the specified ID |
Technical Details
| DOM version | Core Level 2 Document Object |
|---|
Document Object