DOM HTMLElement.focus() Method
The focus() method moves keyboard focus to an element, such as a form field or button.
Description
The focus() method gives the element keyboard focus, so it receives keyboard events and is announced by screen readers. It only works on focusable elements: links, buttons and form controls, or any element with a tabindex attribute (use tabindex="-1" to make something focusable by script but not by Tab).
Move focus thoughtfully: it's important when opening dialogs or showing an error, but moving it unexpectedly can confuse keyboard and screen reader users. The page scrolls to the element unless preventScroll is set.
A short delay is sometimes needed after showing an element before focusing it, because elements that are hidden with display: none or visibility: hidden can't receive focus. The autofocus attribute focuses an element when the page or a dialog opens, without any script.
Syntax
element.focus()
element.focus({ preventScroll, focusVisible })Parameters
- optionsOptional.
preventScroll: truestops the page scrolling to the element, andfocusVisiblecontrols whether a focus ring is shown.
Return Value
None (undefined).
Example
Browser Support
The following information will show you the current browser support for the HTMLElement.focus() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





