Web APIs
Element.querySelector()
querySelector returns the first matching element in the search scope or null. Use valid CSS selector syntax and handle absence before accessing properties.
Formal Syntax & Structure
javascript syntaxjavascript
element.querySelector(selectors)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| selectors | string | CSS selector string. |
Return Value
Element | null
First match or null.
Working Example
Example Snippetjavascript
const root=document.createElement("div"); const p=document.createElement("p"); p.className="note";root.append(p); console.log(root.querySelector(".note")===p,root.querySelector(".missing")===null);