Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท DOM: Events

Focus and blur events

Reference for focus, blur, focusin and focusout: when each fires, which ones bubble, relatedTarget, focus management and how to style focus-within.

Four events report keyboard focus changes. focus fires when an element receives focus, and blur fires when it loses it. Both are fired on the element itself and do not bubble. Their bubbling counterparts are focusin and focusout, which fire at the same moments but travel up through the ancestors.

Only focusable elements take part: form controls, links with href, buttons, anything with a tabindex, and editable regions. The window also receives focus and blur when the browser tab or window gains or loses focus.

JavaScript
input.addEventListener("focus", () => input.select());
form.addEventListener("focusout", validateField); // delegated blur

Try it

Highlight the form group that has focus
Validate on blur and focus an element from code

Syntax

JavaScript
element.addEventListener("focus", (event) => { /* FocusEvent, no bubbling */ });
element.addEventListener("blur", (event) => { /* FocusEvent, no bubbling */ });
element.addEventListener("focusin", (event) => { /* FocusEvent, bubbles */ });
element.addEventListener("focusout", (event) => { /* FocusEvent, bubbles */ });

Event details

Event Bubbles relatedTarget
focus no The element that lost focus, or null.
blur no The element that is receiving focus, or null.
focusin yes The element that lost focus, or null.
focusout yes The element that is receiving focus, or null.

When focus moves from A to B, A gets its blur events before B gets its focus events. Do not depend on the order between the focus/blur pair and the focusin/focusout pair of the same element.

Usage notes

  • Delegation needs focusin/focusout. You can also catch focus and blur in the capture phase on an ancestor, but focusin and focusout are simpler.
  • :focus-within and :focus-visible in CSS. Prefer these selectors over JavaScript when you only need styling. :focus-within matches a parent that contains the focused element.
  • relatedTarget can be null. It is null when focus enters from or goes outside the document (another window or the address bar), and some browsers also hide it in other cases.
  • Programmatic focus. element.focus() fires the same events. focus({ preventScroll: true }) avoids the page jumping. Elements need tabindex="0" or "-1" to be focusable from script.
  • Clicking non-focusable content moves focus to body and fires blur on the previous element.
  • Do not trap people. Never move focus away from a field on blur, and never prevent users from leaving it. Show validation messages instead.
  • Safari and buttons. Some browsers do not focus buttons or checkboxes on click on certain platforms, so a click does not always produce a focus event.
  • Window focus. window.addEventListener("blur", ...) is not a reliable "tab hidden" signal; use visibilitychange for that.

Browser support

focus, blur, focusin and focusout are supported by all current browsers. The platform differences listed above concern how clicks move focus, not the events themselves.