CKTIS / HTML
HTML term or value: autofocus
The `autofocus` attribute asks the browser to focus an element when it becomes available in a document, dialog, or popover; it is neither a guarantee nor a scrolling command.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<input name="query" autofocus>autofocus is a boolean attribute: presence is what matters, so autofocus="false" still enables it. A candidate must pass the focusing steps and belong to the relevant focus scope.
For a dialog, the request is processed when showModal() opens it. Only one candidate may be declared in a focus scope; document.activeElement identifies it after the focusing steps succeed.
Automatic focus can move a user without warning, change a screen reader reading position, and open an on-screen keyboard. Use it only when the next action is unambiguous and the surrounding heading and instructions are already available.
The example puts autofocus only on the first field: its property is true, the second field’s property is false, and document.activeElement has the identifier first. This is one conforming candidate.
Examples
<!doctype html><meta charset="utf-8"><dialog id="d"><input id="first" autofocus><input id="second"></dialog><output id="result"></output><script>d.showModal();result.textContent=`first=${first.autofocus}; second=${second.autofocus}; active=${document.activeElement.id}`;</script>Result
first=true; second=false; active=first
The executed example verifies focus on the sole declared candidate when a modal dialog opens.