JavaScript Reference ยท DOM: Events
The input and change events
Reference for the input, change and beforeinput events: react to typing, selection and checkbox changes, read values, and know when each event fires.
Form controls report changes through two events with a similar sound but different timing:
inputfires immediately every time the value changes: each typed character, paste, deletion, drag-and-drop, voice entry, or movement of a range slider. It also fires for checkboxes, radio buttons,<select>and color pickers when the user changes them.changefires when the user commits a change. For text fields this means when the field loses focus after its value was edited (or on Enter in some cases). For checkboxes, radios,<select>, file inputs and date pickers it fires right away.
A third event, beforeinput, fires before the DOM changes, and it is cancelable, which lets you reject an edit.
JavaScript
field.addEventListener("input", (event) => { console.log(event.target.value); });
Try it
Syntax
JavaScript
control.addEventListener("input", (event) => { /* InputEvent for text-like controls */ }); control.addEventListener("change", (event) => { /* Event */ }); control.addEventListener("beforeinput", (event) => { event.preventDefault(); });
Event details
| Event | Bubbles | Cancelable | Notes |
|---|---|---|---|
beforeinput |
yes | yes (for editable text) | event.data and event.inputType describe the pending edit. |
input |
yes | no | InputEvent for text with data, inputType, isComposing. A plain Event for checkboxes, selects and sliders. |
change |
yes | no | Plain Event. Read the value from event.target. |
Usage notes
- Read values from the element. Use
event.target.valuefor text andselect,checkedfor checkboxes and radios,filesfor file inputs, andvalueAsNumberorvalueAsDatefor numeric and date inputs. - Use
inputfor live feedback such as counters, search-as-you-type (combine with a debounce) and previews. Usechangewhen you only need the final value. - Debounce expensive work.
inputcan fire dozens of times a second while typing. Delay requests withsetTimeout()and reset the timer on each event. - IME composition. While composing text (for example in Japanese),
inputfires repeatedly withisComposing: true. Wait forcompositionendif you must only react to finished text. - Programmatic changes are silent. Setting
element.value = "x"does not fireinputorchange. Dispatch the event yourself if other code must hear it. - Radio groups. The
changeevent fires on the radio that became selected, not on the one that was deselected. - Autofill and paste. Both trigger
input; key events may not fire at all. - Do not block text with keydown. Prefer
beforeinputwithinputType(for exampleinsertText,insertFromPaste,deleteContentBackward) or validate ininputand fix the value.
Browser support
input and change are supported by all browsers. beforeinput is supported by all current browsers, but check a compatibility table before depending on getTargetRanges() or on cancelling edits in editable regions.