-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
96 lines (62 loc) · 2.15 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
title: "Window: focus event"
short-title: focus
slug: Web/API/Window/focus_event
page-type: web-api-event
browser-compat: api.Window.focus_event
---
{{APIRef}}
The **`focus`** event fires when an element has received focus.
The opposite of `focus` is {{domxref("Window/blur_event", "blur")}}.
This event is not cancelable and does not bubble.
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
```js
addEventListener("focus", (event) => {});
onfocus = (event) => {};
```
## Event type
A {{domxref("FocusEvent")}}. Inherits from {{domxref("UIEvent")}} and {{domxref("Event")}}.
{{InheritanceDiagram("FocusEvent")}}
## Event properties
_This interface also inherits properties from its parent {{domxref("UIEvent")}}, and indirectly from {{domxref("Event")}}._
- {{domxref("FocusEvent.relatedTarget")}}
- : An {{domxref("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 {{domxref("EventTarget.addEventListener()", "addEventListener()")}} to monitor `focus` and {{domxref("Window/blur_event", "blur")}} events.
#### HTML
```html
<p id="log">Click on this document to give it focus.</p>
```
#### CSS
```css
.paused {
background: #ddd;
color: #555;
}
```
#### JavaScript
```js
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.";
}
const log = document.getElementById("log");
window.addEventListener("blur", pause);
window.addEventListener("focus", play);
```
#### Result
{{EmbedLiveSample("Live_example")}}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- Related event: {{domxref("Window/blur_event", "blur")}}
- This event on `Element` targets: {{domxref("Element/focus_event", "focus")}} event