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.
input.addEventListener("focus", () => input.select()); form.addEventListener("focusout", validateField); // delegated blur
Try it
Syntax
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 catchfocusandblurin the capture phase on an ancestor, butfocusinandfocusoutare simpler. :focus-withinand:focus-visiblein CSS. Prefer these selectors over JavaScript when you only need styling.:focus-withinmatches a parent that contains the focused element.relatedTargetcan benull. It isnullwhen 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 needtabindex="0"or"-1"to be focusable from script. - Clicking non-focusable content moves focus to
bodyand firesbluron 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
focusevent. - Window focus.
window.addEventListener("blur", ...)is not a reliable "tab hidden" signal; usevisibilitychangefor 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.
Related
- keyboard events
- input and change events
- event delegation
Event.targetandcurrentTargetโrelatedTargetfor focus eventsHTMLElement.focus()- Lesson: Events