-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
66 lines (45 loc) · 1.24 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
---
title: "HTMLInputElement: select event"
short-title: select
slug: Web/API/HTMLInputElement/select_event
page-type: web-api-event
browser-compat: api.HTMLInputElement.select_event
---
{{APIRef}}
The **`select`** event fires when some text has been selected.
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
```js
addEventListener("select", (event) => {});
onselect = (event) => {};
```
## Event type
A generic {{domxref("Event")}}.
## Examples
### Selection logger
```html
<input value="Try selecting some text in this element." />
<p id="log"></p>
```
```js
function logSelection(event) {
const log = document.getElementById("log");
const selection = event.target.value.substring(
event.target.selectionStart,
event.target.selectionEnd,
);
log.textContent = `You selected: ${selection}`;
}
const input = document.querySelector("input");
input.addEventListener("select", logSelection);
```
{{EmbedLiveSample("Selection_logger")}}
### onselect equivalent
You can also set up the event handler using the `onselect` property:
```js
input.onselect = logSelection;
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}