Web APIs
HTMLElement.focus()
Requests keyboard focus for an eligible element. Options may include preventScroll.
Formal Syntax & Structure
javascript syntaxjavascript
element.focus(options?)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| options | object | Optional focus settings such as preventScroll. |
Return Value
undefined
No return value.
Working Example
Example Snippetjavascript
const b=document.createElement("button");b.textContent="Save";document.body.append(b);b.focus();console.log(document.activeElement===b);