HTML onchange Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Input

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

change event

Value committed.

03

Form controls

input, select.

04

vs oninput

Keystroke vs commit.

05

addEventListener

Preferred way.

06

.onchange

Property API.

Purpose of onchange Attribute

The 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.

💡
Prefer addEventListener on the element

Production code should use select.addEventListener("change", handler). Inline onchange on <select> works for tutorials but mixes markup and behavior.

📝 Syntax

Set onchange on a form control or assign element.onchange:

onchange.html
<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>

Syntax Rules

  • Value is JavaScript code executed when the control’s value changes and is committed.
  • Works on form controls: input, textarea, select, checkbox, and radio.
  • On text inputs, change fires after edit + blur; on select, it fires immediately.
  • JavaScript: element.onchange = function() { … }.
  • Modern alternative: element.addEventListener("change", handler).
  • The change event bubbles — you can delegate on <form>.

💎 Values

The onchange attribute accepts a string of JavaScript code:

  • onchange="changeColor()" — Call a named function.
  • onchange="this.form.submit()" — Inline statement (use carefully).
  • JavaScript: field.onchange = () => { … } assigns the handler.
onchange-js.html
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);

});

⚡ Quick Reference

ControlWhen change firesHandler
<select>Immediately on new optiononchange
<input type="text">After edit + bluronchange
checkbox / radioWhen checked state togglesonchange
Live typingEvery keystrokeoninput
Focus leaves fieldAlways on bluronblur
Bubbles?change yesDelegate on form

Applicable Elements

TargetSupported?Notes
<select>YesMost common — fires immediately
<input> (text, email, etc.)YesFires on blur after value changed
<textarea>YesSame commit-on-blur behavior
input type="checkbox" / radioYesFires when selection toggles
<button>, <a>NoNo value change event

onchange vs onblur vs oninput

HandlerWhen it firesTypical use
oninputEvery keystroke / pasteLive search, character count
onchangeValue changed and committedDropdown actions, saved edits
onblurField loses focus (any reason)Validate even if value unchanged

Examples Gallery

Inline onchange on a select dropdown, addEventListener on a text input, and comparing change with oninput.

👀 Live Preview

Pick a theme color — change fires immediately on <select>:

Choose a color to see change fire.

Example — Inline onchange on select

Update the page when the user picks a new option:

inline-onchange.html
<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>
Try It Yourself

How It Works

The browser fires change on the select when the committed value changes. The inline onchange attribute calls your function at that moment.

Dynamic Values with JavaScript

Attach the handler with addEventListener on a text input:

dynamic-onchange.html
<script>

  document.getElementById("city").addEventListener("change", function () {

    console.log("Committed value:", this.value);

  });



  // Or property form:

  document.getElementById("city").onchange = function () { /* … */ };

</script>
Try It Yourself

How It Works

Register the listener once at page load. On a text field, change fires only when the value changed and the user blurs the field.

Example — change vs input

Use input for live feedback; use change for committed values:

change-vs-input.html
field.addEventListener("input", () => {

  counter.textContent = field.value.length + " characters";

});



field.addEventListener("change", () => {

  saveDraft(field.value);

});
Try It Yourself

How It Works

input fires on every keystroke; change fires when the value is committed. Pick the event that matches whether you need live or final values.

♿ Accessibility

  • Announce updates — When change triggers visible UI updates, consider aria-live="polite" on a status region so screen readers are notified.
  • Do not rely on color alone — Pair visual feedback with text so all users understand what changed.
  • Avoid alert() on change — Inline status text near the control is more accessible than modal alerts.
  • Keyboard users — Change fires when keyboard users select options or blur edited fields; test with Tab and arrow keys.
  • Labels — Every control with onchange should have an associated <label> or aria-label.

🧠 How onchange Works

1

User edits control

Types, picks option, or toggles checkbox.

Input
2

Value is committed

Select/checkbox: immediately. Text: on blur.

Commit
3

change event fires

Handler runs on element.

onchange
=

Update UI or data

React to new value.

Browser Support

The change event and onchange handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge. Behavior is consistent for standard form controls.

DOM Events · Fully supported

Universal onchange support

All major browsers fire the underlying event and honor the onchange handler attribute.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
onchange attribute 99% supported

Bottom line: Universal support on form controls. Test select vs text input timing — change fires immediately on dropdowns, on blur for text fields.

💡 Best Practices

✅ Do

  • Use addEventListener on the element — Clearer than inline onchange.
  • Pick the right event — Use input for live typing; change for committed values.
  • Remember select timing — Change on <select> fires immediately, not on blur.
  • Keep handlers fast — Avoid slow network calls blocking the UI after selection.
  • Delegate on forms — change bubbles — one listener on <form> can handle many fields.

❌ Don’t

  • Do not auto-submit blindly — Submitting on every select change can surprise users; confirm intent when needed.

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onchange

Bookmark these before wiring your next event handler.

5
Core concepts
🌐 02

Form controls

input, select.

Scope
🔄 03

vs oninput

Commit vs live.

Pattern
📝 04

addEventListener

Preferred.

Modern
05

Bubbles

Delegate on form.

Note

❓ Frequently Asked Questions

It runs JavaScript when the change 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.
Prefer element.addEventListener("change", …). Inline onchange works for simple demos.
When the value changed and the user blurs the field (tabs away or clicks elsewhere). It does not fire on every keystroke.

React to form value changes

Practice inline onchange, addEventListener, and change vs input in the Try It editor.

Try onchange demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful