Focus events
Focus events, such as focus and blur, fire when an element gets or loses focus.
Overview
Focus events, such as focus and blur, fire when an element gets or loses focus.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 26 | 12 | 24 | 7 | 26 | 7 | |
| The blur event fires when an element has lost focus. The event does not bubble, but the related Element/focusout_event event that follows does bubble. | 1 | 12 | 24 | 3.1 | 18 | 2 |
| The focus event fires when an element has received focus. The event does not bubble, but the related Element/focusin_event event that follows does bubble. | 1 | 12 | 24 | 3.1 | 18 | 2 |
| The focusin event fires when an element has received focus, after the Element/focus_event event. The two events differ in that focusin bubbles, while focus does not. | 1 | 12 | 52 | 5 | 18 | 4.2 |
| The focusout event fires when an element has lost focus, after the Element/blur_event event. The two events differ in that focusout bubbles, while blur does not. | 1 | 12 | 52 | 5 | 18 | 4.2 |
| The FocusEvent() constructor returns a newly created FocusEvent object with an optional EventTarget. When the event has both a source and a destination, the relatedTarget value must be set to the other target. | 26 | 12 | 24 | 7 | 26 | 7 |
| The relatedTarget read-only property of the FocusEvent interface is the secondary target, depending on the type of event: | 26 | 12 | 24 | 7 | 26 | 7 |
| The HTMLElement.blur() method removes keyboard focus from the current element. | 1 | 12 | 1.5 | 3 | 18 | 1 |
| The HTMLElement.focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will receive keyboard and similar events by default. | 1 | 12 | 1.5 | 3 | 18 | 1 |
focus (options focusVisible parameter) `options.focusVisible` parameter | 145 | 145 | 104 | 18.4 | 145 | 18.4 |
focus (options preventScroll parameter) `options.preventScroll` parameter | 64 | 17 | 68 | 15 | | 15.5 |
- This feature was removed in a later browser version (24)
- The interface for this event is `Event`, not `FocusEvent`.
- This browser only partially implements this feature
- This feature was removed in a later browser version (24)
- The interface for this event is `Event`, not `FocusEvent`.
- This browser only partially implements this feature
- The `onfocusin` event handler property is not supported. To listen to this event, use `element.addEventListener('focusin', function() {});`.
- This browser only partially implements this feature
- The `onfocusin` event handler property is not supported. To listen to this event, use `element.addEventListener('focusin', function() {});`.
- This browser only partially implements this feature
- The `onfocusin` event handler property is not supported. To listen to this event, use `element.addEventListener('focusin', function() {});`.
- This browser only partially implements this feature
- The `onfocusin` event handler property is not supported. To listen to this event, use `element.addEventListener('focusin', function() {});`.
- This browser only partially implements this feature
- The `onfocusout` event handler property is not supported. To listen to this event, use `element.addEventListener('focusout', function() {});`.
- This browser only partially implements this feature
- The `onfocusout` event handler property is not supported. To listen to this event, use `element.addEventListener('focusout', function() {});`.
- This browser only partially implements this feature
- The `onfocusout` event handler property is not supported. To listen to this event, use `element.addEventListener('focusout', function() {});`.
- This browser only partially implements this feature
- The `onfocusout` event handler property is not supported. To listen to this event, use `element.addEventListener('focusout', function() {});`.
- If there's no hardware keyboard connected and the user has not yet interacted with the page, then calling `focus()` on an `<input>` element has no effect (for example, the element does not match the `:focus` selector).
Syntax
const input = document.querySelector('input');
input.addEventListener('focus', () => {
input.style.borderColor = 'blue';
});
input.addEventListener('blur', () => {
input.style.borderColor = '';
}); Live demo
Focusin / Focusout(baring with)
Focus/blur is baring that, focusin/focusout is baring.
Use cases
Using Focus events
Focus events, such as focus and blur, fire when an element gets or loses focus.
Cautions
- May not be supported in older browsers.
Accessibility
- Verify how this element is announced by screen readers.
Related links
Powered by web-features