This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
The HTMLElement.focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will receive keyboard and similar events by default.
By default the browser will scroll the element into view after focusing it, and it may also provide visible indication of the focused element (typically by displaying a "focus ring" around the element). Parameter options are provided to disable the default scrolling and force visible indication on elements.
focus() focus(options)
options OptionalAn optional object for controlling aspects of the focusing process. This object may contain the following properties:
preventScroll OptionalA boolean value indicating whether or not the browser should scroll the document to bring the newly-focused element into view. A value of false for preventScroll (the default) means that the browser will scroll the element into view after focusing it. If preventScroll is set to true, no scrolling will occur.
focusVisible Optional Experimental A boolean value that should be set to true to force, or false to prevent visible indication that the element is focused. If the property is not specified, a browser will provide visible indication if it determines that this would improve accessibility for users.
None (undefined).
This example uses a button to set the focus on a text field.
<input id="myTextField" value="Text field." /> <button id="focusButton">Click to set focus on the text field</button>
The code below adds an event handler to set the focus on the text field when the button is pressed. Note that most browsers will automatically add visible indication (a "focus ring") for a focused text field, so the code does not set focusVisible to true.
document.getElementById("focusButton").addEventListener("click", () => {
document.getElementById("myTextField").focus();
});
Select the button to set focus on the text field.
This example demonstrates how you can set the focus on a button element.
First we define three buttons. Both the middle and right button will set focus on the left-most button. The right-most button will also specify focusVisible.
<button id="myButton">Button</button> <button id="focusButton">Click to set focus on "Button"</button> <button id="focusButtonVisibleIndication"> Click to set focus and focusVisible on "Button" </button>
The code below sets up handlers for click events on the middle and right buttons.
document.getElementById("focusButton").addEventListener("click", () => {
document.getElementById("myButton").focus();
});
document
.getElementById("focusButtonVisibleIndication")
.addEventListener("click", () => {
document.getElementById("myButton").focus({ focusVisible: true });
});
Select either the middle button or right-most button to set focus on the left-most button.
Browsers do not usually show visible focus indication on button elements when focus is set programmatically, so the effect of selecting the middle button may not be obvious. However provided the focusVisible option is supported on your browser, you should see focus changing on the left-most button when the right-most button is selected.
This example shows the effect of setting focus with the option preventScroll set true and false (the default).
The HTML defines two buttons that will be used to set the focus of a third button that is off-screen
<button id="focus_scroll">Click to set focus on off-screen button</button> <button id="focus_no_scroll"> Click to set focus on offscreen button without scrolling </button> <div id="container"> <button id="myButton">Button</button> </div>
This code sets a click event handler on the first and second buttons to set the focus on the last button. Note that the first handler doesn't specify the preventScroll option so scrolling to the focused element will be enabled.
document.getElementById("focus_scroll").addEventListener("click", () => {
document.getElementById("myButton").focus(); // default: {preventScroll:false}
});
document.getElementById("focus_no_scroll").addEventListener("click", () => {
document.getElementById("myButton").focus({ preventScroll: true });
});
Select the first button to set focus and scroll to the off-screen button. Selecting the second button set's the focus, but scrolling is disabled.
| Specification |
|---|
| HTML> # dom-focus-dev> |
HTMLElement.focus() from a mousedown event handler, you must call event.preventDefault() to keep the focus from leaving the HTMLElement
tabindex or shadow dom, which previously remained under-specified, were updated in October 2019. See the WHATWG blog for more information.| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
focus |
1 | 12 | 1.5 | 8 | 3 | 18 | 4 | 10.1 | 1If there's no hardware keyboard connected and the user has not yet interacted with the page, then callingfocus() on an <input> element has no effect (for example, the element does not match the :focus selector). |
1.0 | 4.4 | 1If there's no hardware keyboard connected and the user has not yet interacted with the page, then callingfocus() on an <input> element has no effect (for example, the element does not match the :focus selector). |
options_focusVisible_parameter |
No | No | 104 | No | 18.4 | No | 104 | No | 18.4 | No | No | 18.4 |
options_preventScroll_parameter |
64 | 17 | 68 | 51 | 15 | No | No | No | 15.5 | No | No | 15.5 |
HTMLElement.blur to remove the focus from an element.document.activeElement to know which is the currently focused element.
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus