👀 Live Preview
Pick a theme color — change fires immediately on <select>:
Choose a color to see change fire.

The onchange attribute is an inline event handler that runs JavaScript when the change event fires. That happens when the user commits a new value on a form control—for example picking a different option from a dropdown or finishing edits in a text field. It is widely used for updating UI, filtering lists, or reacting to form selections. On <select> and checkboxes, change fires immediately; on text inputs it usually fires when the field loses focus after the value changed. The change event bubbles, so you can listen on a parent form with addEventListener.
Inline JS.
Value committed.
input, select.
Keystroke vs commit.
Preferred way.
Property API.
onchange AttributeThe primary purpose of onchange is to react when the user picks or enters a new committed value—without waiting for form submit. Common uses include updating a preview when a color is selected, loading related options when a category changes, syncing hidden fields, or enabling a submit button only after required choices are made. Because change only fires when the value actually changed, it avoids redundant work on focus moves that do not edit the field.
Do not confuse onchange with oninput. The input event fires on every keystroke; change waits until the value is committed. On text fields that usually means blur after editing. On <select>, change fires as soon as the user picks a new option.
Production code should use select.addEventListener("change", handler). Inline onchange on <select> works for tutorials but mixes markup and behavior.
Set onchange on a form control or assign element.onchange:
<script>
function changeColor() {
const color = document.getElementById("color").value;
document.body.style.backgroundColor = color;
}
</script>
<label for="color">Theme</label>
<select id="color" onchange="changeColor()">
<option value="#f8fafc">Default</option>
<option value="#eff6ff">Blue</option>
</select>input, textarea, select, checkbox, and radio.select, it fires immediately.element.onchange = function() { … }.element.addEventListener("change", handler).change event bubbles — you can delegate on <form>.The onchange attribute accepts a string of JavaScript code:
onchange="changeColor()" — Call a named function.onchange="this.form.submit()" — Inline statement (use carefully).field.onchange = () => { … } assigns the handler.const city = document.getElementById("city");
city.addEventListener("change", () => {
console.log("Committed value:", city.value);
});
// On select — fires immediately when option changes
document.getElementById("country").addEventListener("change", (e) => {
loadRegions(e.target.value);
});| Control | When change fires | Handler |
|---|---|---|
<select> | Immediately on new option | onchange |
<input type="text"> | After edit + blur | onchange |
checkbox / radio | When checked state toggles | onchange |
| Live typing | Every keystroke | oninput |
| Focus leaves field | Always on blur | onblur |
| Bubbles? | change yes | Delegate on form |
| Target | Supported? | Notes |
|---|---|---|
<select> | Yes | Most common — fires immediately |
<input> (text, email, etc.) | Yes | Fires on blur after value changed |
<textarea> | Yes | Same commit-on-blur behavior |
input type="checkbox" / radio | Yes | Fires when selection toggles |
<button>, <a> | No | No value change event |
onchange vs onblur vs oninput| Handler | When it fires | Typical use |
|---|---|---|
oninput | Every keystroke / paste | Live search, character count |
onchange | Value changed and committed | Dropdown actions, saved edits |
onblur | Field loses focus (any reason) | Validate even if value unchanged |
Inline onchange on a select dropdown, addEventListener on a text input, and comparing change with oninput.
Pick a theme color — change fires immediately on <select>:
Choose a color to see change fire.
Update the page when the user picks a new option:
<label for="color">Theme color</label>
<select id="color" onchange="changeColor()">
<option value="red">Red</option>
<option value="green">Green</option>
</select>
<script>
function changeColor() {
const value = document.getElementById("color").value;
document.body.style.backgroundColor = value;
}
</script>The browser fires change on the select when the committed value changes. The inline onchange attribute calls your function at that moment.
Attach the handler with addEventListener on a text input:
<script>
document.getElementById("city").addEventListener("change", function () {
console.log("Committed value:", this.value);
});
// Or property form:
document.getElementById("city").onchange = function () { /* … */ };
</script>Register the listener once at page load. On a text field, change fires only when the value changed and the user blurs the field.
Use input for live feedback; use change for committed values:
field.addEventListener("input", () => {
counter.textContent = field.value.length + " characters";
});
field.addEventListener("change", () => {
saveDraft(field.value);
});input fires on every keystroke; change fires when the value is committed. Pick the event that matches whether you need live or final values.
aria-live="polite" on a status region so screen readers are notified.onchange should have an associated <label> or aria-label.Types, picks option, or toggles checkbox.
Select/checkbox: immediately. Text: on blur.
Handler runs on element.
React to new value.
The change event and onchange handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge. Behavior is consistent for standard form controls.
onchange supportAll major browsers fire the underlying event and honor the onchange handler attribute.
Bottom line: Universal support on form controls. Test select vs text input timing — change fires immediately on dropdowns, on blur for text fields.
onchange.input for live typing; change for committed values.<select> fires immediately, not on blur.change bubbles — one listener on <form> can handle many fields.The onchange attribute runs JavaScript when a form control’s value changes and the change event fires. Use it to update UI when users pick dropdown options, toggle checkboxes, or finish editing text fields.
Prefer addEventListener("change", …), know the difference from oninput and onblur, and remember that change fires immediately on <select> but on blur for text inputs.
onchangeBookmark these before wiring your next event handler.
change fires.
Eventinput, select.
ScopeCommit vs live.
PatternPreferred.
ModernDelegate on form.
Notechange event fires — when the user commits a new value on a form control.input, textarea, select, checkbox, and radio. Not buttons or plain links.oninput fires on every keystroke. onchange fires when the value is committed — on blur for text fields, immediately for select.onblur fires every time focus leaves. onchange fires only when the value actually changed.element.addEventListener("change", …). Inline onchange works for simple demos.Practice inline onchange, addEventListener, and change vs input in the Try It editor.
5 people found this page helpful