Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
108 lines (75 loc) · 2.44 KB
/
Copy pathindex.md
File metadata and controls
108 lines (75 loc) · 2.44 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
---
title: TextEvent
slug: Web/API/TextEvent
page-type: web-api-interface
status:
- deprecated
browser-compat: api.TextEvent
---
{{APIRef("UI Events")}}
The **`TextEvent`** interface is a legacy UI event interface for reporting changes to text UI elements.
> [!NOTE]
> `TextEvent` events have been superseded by events such as `input`, `beforeinput`, `keypress`, `keyup`, and `keydown`.
{{InheritanceDiagram}}
## Instance properties
_This interface also inherits properties from its parent {{domxref("UIEvent")}}, and indirectly from {{domxref("Event")}}._
- {{domxref("TextEvent.data")}} {{ReadOnlyInline}} {{deprecated_inline}}
- : Indicates the data associated with the event.
## Instance methods
- {{domxref("TextEvent.initTextEvent()")}} {{deprecated_inline}}
- : Populates the values of this (new) `TextEvent` with the given parameters.
## Events list
The following is a list of all `TextEvent` events:
- `textinput`
## Examples
### Listen for text input events
You can register a listener for text input events using {{DOMxRef("EventTarget.addEventListener()")}} as follows:
```js
element.addEventListener("textInput", (event) => {
// …
});
```
### Simple logger showing input events
This example listens for a number of events that are fired on an input, including `textInput`.
The event type and the event data are logged, allowing you to see where `textInput` is emitted relative to the other events such as those generated by key presses.
#### HTML
```html
<input placeholder="Enter some text" name="name" />
```
```html hidden
<pre id="log"></pre>
```
```css hidden
#log {
height: 140px;
overflow: scroll;
padding: 0.5rem;
border: 1px solid black;
}
```
#### JavaScript
```js hidden
const logElement = document.querySelector("#log");
function log(text) {
logElement.innerText = `${logElement.innerText}${text}\n`;
logElement.scrollTop = logElement.scrollHeight;
}
```
```js
const input = document.querySelector("input");
input.addEventListener("keypress", updateValue);
input.addEventListener("keyup", updateValue);
input.addEventListener("keydown", updateValue);
input.addEventListener("input", updateValue);
input.addEventListener("beforeinput", updateValue);
input.addEventListener("textInput", updateValue);
function updateValue(e) {
log(`${e.type}: ${e.data}`);
}
```
#### Result
{{EmbedLiveSample("Simple logger showing input events", "100%", "210px" )}}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}