Favorites

DOM HTMLElement.focus() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
HTMLElement.focus()
DOM HTMLElement MethodThis method is supported by all modern browsers.
Updated 28th November 2026

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: true stops the page scrolling to the element, and focusVisible controls whether a focus ring is shown.

Return Value

None (undefined).

Example

<p>Email is required. <button id="fix" type="button">Go to field</button></p>
<input id="email" type="email" placeholder="you@example.com">
const email = document.getElementById('email');
document.getElementById('fix').addEventListener('click', () => {
  email.focus();
  console.log('Focused:', document.activeElement.id);
});
document.getElementById('fix').click();

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee