.blur()

Term side of the Card.
Topic: Object on Page
Unit title: .blur()
Categories: Method
Explanation side of the Card

Explanation: The browser API method that removes focus from an element, like an input field. It's often used to indicate that the user has finished interacting with the element.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.5.
  • The safari browser supports this feature starting from version 3.
Availability: 

Example

Wrongjs
menuItem.addEventListener('click', () => {
  closeMenu();
  menuItem.blur();
});
Rightjs
menuItem.addEventListener('click', () => {
  closeMenu();
  menuTrigger.focus();
});

Blurring drops focus to the body, so keyboard users lose their place; move focus to a meaningful element.

Common mistakes

  • Calling blur() after a click to hide the outline, which breaks keyboard navigation.
  • Expecting the blur event to bubble for delegation; focusout does.

Key points

  • Focus is moved to a meaningful element with focus() instead of being dropped with blur().
  • Focus rings are not hidden by calling blur() on click; :focus-visible styling is used instead.

Interview questions

blur vs focusout?

Both fire when an element loses focus, but focusout bubbles and blur does not, so event delegation needs focusout.

Where does focus go after blur()?

To the document body: document.activeElement becomes document.body.

How do I hide the focus ring on mouse click?

Style :focus-visible instead of :focus. Browsers usually skip the ring for mouse clicks on buttons and keep it for keyboard users.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy