JavaScriptDOM and events

HTMLElement.focus()

Sets focus on an element, such as a form field, so it immediately receives keyboard input. The blur() method removes focus.

Returns
undefined
On this page

Example

#
JavaScript
const email = document.querySelector('#email')

email.focus()
console.log(document.activeElement === email)
console.log(document.activeElement.id)

email.blur()
console.log(document.activeElement === document.body)
Result
true
email
true

This example doesn’t run in the preview, because it would move focus away from this page and into the example’s frame. On a page with an <input id="email"> field, it prints the result shown above. After blur(), focus returns to the document, and document.activeElement points to <body>.

Definition and usage

#

The focus() method moves focus to an element, meaning it’s the one that receives key presses. A focused text field shows a blinking cursor, and a focused button reacts to Enter and space. The blur() method does the opposite and removes focus. You can read the currently focused element from document.activeElement.

Focus can go to interactive elements: form fields, buttons, links with an href attribute, and elements with a tabindex attribute. Calling focus() on a plain div or heading does nothing, unless you add tabindex="-1" to it. Such an element can be activated from code, but it doesn’t join the Tab key’s navigation order.

When focus changes, an element gets the focus and blur events, which don’t bubble, and focusin and focusout, which bubble and are suited to delegation. By default, the browser scrolls the page so the focused element is visible. The preventScroll: true option turns off that scrolling.

Thoughtful use of focus() improves accessibility: after opening a dialog, move focus into its first field; after closing it, return focus to the button that opened it; and after a failed form submission, point to the first invalid field. Don’t move focus without a clear reason, though, since people using the keyboard and screen readers lose their place when you do.

Syntax

#
Syntax
element.focus()
element.focus(options)
element.blur()

document.activeElement

Parameters

#
  • options

    optional object

    preventScroll: true sets focus without scrolling the page to the element.

More examples

#
Focus after clicking a button
JavaScript
const reply = document.querySelector('#reply')
const comment = document.querySelector('#comment')

reply.addEventListener('click', () => comment.focus())
comment.addEventListener('focus', () => console.log('Comment field active'))
comment.addEventListener('blur', () => console.log('Comment field inactive'))

console.log('Active element:', document.activeElement)
Console
Active element: <body>

At the start, focus belongs to the document itself, i.e. <body>. Click “Reply”, and focus will move to the comment field. Then click next to the field to see the blur event.

Focus on a heading after loading a lesson
JavaScript
const heading = document.querySelector('#lesson-title')

heading.setAttribute('tabindex', '-1')
heading.focus({ preventScroll: true })

console.log(document.activeElement.tagName)
console.log(heading.tabIndex)
Result
H2
-1

On a page with the heading <h2 id="lesson-title">, the heading normally doesn’t accept focus. tabindex="-1" lets you activate it from code, for example after loading a new lesson, so a screen reader immediately reads out its title. The element doesn’t join the Tab key’s order, though.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • After opening a dialog, move focus into it, and return it to the button that opened it after closing. People using the keyboard won’t lose their place on the page that way.
  • Use focusin and focusout for delegating focus events. The focus and blur events don’t bubble to parents.
  • Style the focus look in CSS with :focus-visible. The outline will appear with keyboard input, not after every mouse click.

Related entries

#

Spotted a mistake or missing an example? Write to us.