HTML DOM focus() method
Example
document.getElementById(
"myAnchor"
).focus();
Try it
Definition and usage
The focus() method is used to set focus on an element (if it can be set).
**Tip:** Use blur() method to remove focus from the element.
Browser support
| Method | |||||
|---|---|---|---|---|---|
| focus() | Yes | Yes | Yes | Yes | Yes |
Syntax
HTMLElementObject
.focus()
Parameters
| None |
|---|
Technical description
| Return Value: | No return value |
|---|
More examples
Example
Set focus on the text field:
document.getElementById(
"myText"
).focus();
Try it »
Example
After the document is loaded, set focus on the text field:
window.onload =
function
() {
document.getElementById(
"myText"
).focus();
};
Try it »
Related pages
HTML DOM Reference Manual: onfocus event
Element object