Skip to content
Tutorials Center
My learning

Topics

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:

  • input fires 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.
  • change fires 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

Live counter with input, final value with change
Checkbox, select and range

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.value for text and select, checked for checkboxes and radios, files for file inputs, and valueAsNumber or valueAsDate for numeric and date inputs.
  • Use input for live feedback such as counters, search-as-you-type (combine with a debounce) and previews. Use change when you only need the final value.
  • Debounce expensive work. input can fire dozens of times a second while typing. Delay requests with setTimeout() and reset the timer on each event.
  • IME composition. While composing text (for example in Japanese), input fires repeatedly with isComposing: true. Wait for compositionend if you must only react to finished text.
  • Programmatic changes are silent. Setting element.value = "x" does not fire input or change. Dispatch the event yourself if other code must hear it.
  • Radio groups. The change event 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 beforeinput with inputType (for example insertText, insertFromPaste, deleteContentBackward) or validate in input and 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.