HTML global attributeGlobal attributes

onchange

Runs JavaScript code when the user commits a new field value: picks an option, ticks a checkbox or finishes typing.

Applies to
<input>, <select> and <textarea>. The event bubbles, so the attribute also works on <form>
On this page

Example

#
HTML
<label for="course">Choose a course</label>
<select id="course" onchange="
  const course = this.selectedOptions[0].text;
  document.getElementById('choice').textContent = 'Chosen course: ' + course;
">
  <option value="">Nothing chosen</option>
  <option value="html">HTML and CSS</option>
  <option value="js">JavaScript</option>
  <option value="python">Python</option>
</select>
<p id="choice">Chosen course: none</p>

<label>
  <input type="checkbox" onchange="document.body.style.fontSize = this.checked ? '18px' : ''">
  Larger font in the preview
</label>
Preview

The list reacts right after an option is chosen, and the checkbox after every tick and untick. The field’s state is read with this.checked.

Definition and usage

#

The onchange attribute reacts to the change event, which fires when the user commits a new field value. In <select> lists, checkboxes, radio buttons and file fields this happens right after the choice. In text fields only when the user leaves the field or presses Enter, and the value differs from the one before editing. In a <textarea> Enter adds a new line, so only leaving the field counts there.

That is why onchange suits settings and filters: a sort order change, a dark mode switch, choosing a file to upload. To react to every typed character, oninput is better. With a range slider, the change event fires only after you release the handle, and input the whole time you drag.

In the attribute’s code, this points to the field. You read the value from this.value, a checkbox’s state from this.checked, and the chosen files from this.files. Like input, the change event bubbles up the tree and does not fire when a script changes the value. In larger projects, add the handling through addEventListener("change", …).

Syntax

#
Syntax
<select onchange="JavaScript code">…</select>

Available in the attribute’s code

#
  • this.value

    The field’s value or the value attribute of the chosen list option.
  • this.checked

    true or false in checkboxes and radio buttons.
  • this.selectedOptions

    The chosen options of a <select> list. You read the text of the first one through this.selectedOptions[0].text.
  • this.files

    The files chosen in <input type="file">, each with a name, size and type.

More examples

#
Comparing the input and change events
HTML
<label for="city">City</label>
<input id="city" type="text"
  oninput="document.getElementById('input-log').textContent = this.value"
  onchange="document.getElementById('change-log').textContent = this.value">
<p>input: <strong id="input-log"></strong></p>
<p>change: <strong id="change-log"></strong></p>
Preview

Type a city name. The “input” line changes with every character, and “change” only when you press Enter or click outside the field.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Do not send the user to another page right after an option is chosen from a list. Someone browsing the list with the keyboard arrows can fire change on every option. Add a “Go” button.
  • In a file field, check the file’s size and type in onchange before you send it. It is a convenience for the user, but the server still has to check the file again.

Related entries

#

Spotted a mistake or missing an example? Write to us.