Web APIs
Document.getElementById()
Find a descendant by its exact ID, without a CSS selector. IDs should be unique in the document. Example console output: true true.
Formal Syntax & Structure
javascript syntaxjavascript
document.getElementById(id)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| id | string | Exact ID value. |
Return Value
Element or null
Matching element or null.
Working Example
Example Snippetjavascript
const p=document.createElement("p"); p.id="stock"; document.body.append(p); console.log(document.getElementById("stock")===p,document.getElementById("missing")===null);