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>
Example
#<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>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
#<select onchange="JavaScript code">…</select>Available in the attribute’s code
#this.value
The field’s value or thevalueattribute of the chosen list option.this.checked
trueorfalsein checkboxes and radio buttons.this.selectedOptions
The chosen options of a<select>list. You read the text of the first one throughthis.selectedOptions[0].text.this.files
The files chosen in<input type="file">, each with a name, size and type.
More examples
#<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>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
changeon every option. Add a “Go” button. - In a file field, check the file’s size and type in
onchangebefore you send it. It is a convenience for the user, but the server still has to check the file again.
Related entries
#- oninputRuns JavaScript code on every change of a field’s value: after typing a character, pasting text or moving a slider.
- <select>A dropdown list the user picks one or more options from.
- <input>A form field: text, email, password, number, date, slider, checkbox, file and many more.
- onsubmitRuns JavaScript code right before a form is submitted. Lets you stop the submission and handle the data in a script.
Spotted a mistake or missing an example? Write to us.