HTML DOM Element focus()
Example
Give focus to an element:
document.getElementById("myAnchor").focus();
Try it Yourself »
Give focus to a text field:
document.getElementById("myText").focus();
Try it Yourself »
Give focus to a text field when the document has been loaded:
window.onload = function() {
document.getElementById("myText").focus();
}
Try it Yourself »
Definition and Usage
The focus()
method gives focus to an element (if it can be focused).
Syntax
element.focus()
Parameters
NONE |
Return Value
NONE |
Browser Support
element.focus()
is a DOM Level 2 (2001) feature.
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |