Element: focusin event
UI Events
UI Events
The focusin event fires when an element has received focus, after the focus event. The two events differ in that focusin bubbles, while focus does not.
The opposite of focusin is the focusout event, which fires when the element has lost focus.
The focusin event is not cancelable.
Syntax
Use the event name in addEventListener():
addEventListener("focusin", (event) => { })
Alternatively, assign to the event handler property of the interface:
onfocusin = (event) => { }
Event type
A FocusEvent. Inherits from UIEvent and Event.
FocusEvent
Examples
Live example
HTML
<form id="form">
<label>
Some text:
<input type="text" placeholder="text input" />
</label>
<label>
Password:
<input type="password" placeholder="password" />
</label>
</form>
JavaScript
const form = document.getElementById("form");
form.addEventListener("focusin", (event) => {
event.target.style.background = "pink";
});
form.addEventListener("focusout", (event) => {
event.target.style.background = "";
});
Result
100%
Specifications
[!NOTE] The UI Events specification describes an order of focus events that's different from what current browsers implement.
Browser compatibility
See also
- Related events: blur, focus, focusout
- Focusing: focus/blur