Window: focus event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The focus event fires when an element has received focus.

The opposite of focus is blur.

This event is not cancelable and does not bubble.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("focus", (event) => { })

onfocus = (event) => { }

Event type

A FocusEvent. Inherits from UIEvent and Event.

Event UIEvent FocusEvent

Event properties

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

FocusEvent.relatedTarget

An EventTarget representing a secondary target for this event. In some cases (such as when tabbing in or out a page), this property may be set to null for security reasons.

Examples

Live example

This example changes the appearance of a document when it loses focus. It uses addEventListener() to monitor focus and blur events.

HTML

html
<p id="log">Click on this document to give it focus.</p>

CSS

css
.paused {
  background: #dddddd;
  color: #555555;
}

JavaScript

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

function pause() {
  document.body.classList.add("paused");
  log.textContent = "FOCUS LOST!";
}

function play() {
  document.body.classList.remove("paused");
  log.textContent =
    "This document has focus. Click outside the document to lose focus.";
}

window.addEventListener("blur", pause);
window.addEventListener("focus", play);

Result

Specifications

Specification
UI Events
# event-type-focus
HTML
# handler-onfocus

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
focus event
Chrome – Full support
Chrome 5 (Release date: 2010-05-25)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 6 (Release date: 2011-08-16)
footnote
footnote Apart from firing the event on window as other browsers do, Firefox also fires the event on the document object. See bug 1228802.
footnote Before Firefox 24, the interface for this event is a plain Event, not FocusEvent.
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
footnote Full support
Safari – Full support
Safari 5.1 (Release date: 2011-07-20)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 6 (Release date: 2011-08-16)
footnote
footnote Apart from firing the event on window as other browsers do, Firefox for Android also fires the event on the document object. See bug 1228802.
footnote Before Firefox for Android 24, the interface for this event is a plain Event, not FocusEvent.
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 5 (Release date: 2011-10-12)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 5 (Release date: 2011-10-12)
footnote Full support
Deno – No support
Deno
footnote No support

Legend

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

Full support
Full support
No support
No support
See implementation notes.

See also

  • Related event: blur
  • This event on Element targets: focus event