Element: focusin event

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 methods like addEventListener().

js
addEventListener("focusin", (event) => {});

Event type

Event properties

This interface also inherits properties from its parent UIEvent, and indirectly from Event.

FocusEvent.relatedTarget

The element losing focus, if any.

Examples

Live example

HTML

html
<form id="form">
  <label>
    Some text:
    <input type="text" placeholder="text input" />
  </label>
  <label>
    Password:
    <input type="password" placeholder="password" />
  </label>
</form>

JavaScript

js
const form = document.getElementById("form");

form.addEventListener("focusin", (event) => {
  event.target.style.background = "pink";
});

form.addEventListener("focusout", (event) => {
  event.target.style.background = "";
});

Result

Specifications

Specification
UI Events
# event-type-focusin

Note: The UI Events specification describes an order of focus events that's different from what current browsers implement.

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
focusin event

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support

See also