/
index.md
59 lines (39 loc) · 1.57 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
---
title: "Document: selectionchange event"
short-title: selectionchange
slug: Web/API/Document/selectionchange_event
page-type: web-api-event
browser-compat: api.Document.selectionchange_event
---
{{APIRef}}
The **`selectionchange`** event of the [Selection API](/en-US/docs/Web/API/Selection) is fired when the current {{domxref("Selection")}} of a {{domxref("Document")}} is changed.
This event is not cancelable and does not bubble.
The event can be handled by adding an event listener for `selectionchange` or using the `onselectionchange` event handler.
> **Note:** This event is not quite the same as the `selectionchange` events fired when the text selection in an {{HTMLElement("input")}} or {{HTMLElement("textarea")}} element is changed. See {{domxref("HTMLInputElement.selectionchange_event")}} for more details.
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
```js
addEventListener("selectionchange", (event) => {});
onselectionchange = (event) => {};
```
## Event type
A generic {{domxref("Event")}}.
## Examples
```js
// addEventListener version
document.addEventListener("selectionchange", () => {
console.log(document.getSelection());
});
// onselectionchange version
document.onselectionchange = () => {
console.log(document.getSelection());
};
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{domxref("Node/selectstart_event", "selectstart")}}
- {{domxref("Document.getSelection()")}}
- {{domxref("Selection", "Selection")}}