This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The DOM beforeinput
event fires when the value of an <input>
, <select>
, or <textarea>
element is about to be modified. The event also applies to elements with contenteditable
enabled, and to any element when designMode
is turned on.
In the case of contenteditable
and designMode
, the event target is the editing host. If these properties apply to multiple elements, the editing host is the nearest ancestor element whose parent isn't editable.
Bubbles | Yes |
---|---|
Cancelable | Yes |
Interface | InputEvent |
Event handler property | None |
Sync / Async | Sync |
Composed | Yes |
Default Action | Update the DOM element |
Examples
This example logs current value of the element immediately before replacing that value with the new one applied to the <input>
element.
HTML
<input placeholder="Enter some text" name="name"/> <p id="values"></p>
JavaScript
const input = document.querySelector('input'); const log = document.getElementById('values'); input.addEventListener('beforeinput', updateValue); function updateValue(e) { log.textContent = e.target.value; }
Result
Specifications
Specification | Status |
---|---|
UI Events The definition of 'beforeinput event' in that specification. |
Working Draft |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
beforeinput event | Chrome Full support Yes | Edge Full support 79 | Firefox No support No | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android No support No | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
See also
- Related event:
input