HTML onchange Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onchange attribute is an event-handler attribute for form controls such as <input>, <select>, and <textarea>. It runs JavaScript when the user commits a change to the control’s value.

Unlike oninput (often every keystroke), change waits until the change is committed — for example choosing an option, toggling a checkbox, or leaving a text field after editing.

Baseline · Widely available.

Not deprecated and not experimental. The change event is stable across modern browsers since about July 2015.

Prefer addEventListener in apps.

Inline onchange is fine for learning. Production code usually uses el.addEventListener("change", handler) so logic stays in script and multiple listeners are easy.

HTML Syntax

syntax.html
<input|select|textarea onchange=" script">

script is JavaScript that runs when change fires. Equivalent JS: el.onchange = handler or el.addEventListener("change", handler).

Attribute Values

The attribute value is always a script. When it runs depends on the control:

Value / controlDescriptionTry it
scriptAny JavaScript run when the user commits a value changeTry it
<select>Fires when the user picks a different optionTry it
Text / textareaUsually after the value changes and the control loses focusTry it
Checkbox / radioCheckbox: check or uncheck. Radio: when checked (not when unchecked)Try it
vs oninputinput is often live; change is committedTry it

Live Preview

Change the control, then watch the status. For text, leave the field (Tab or click away) after typing.

Control

Status updates only when change fires — not on every key for text.

Form control

Waiting for change…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Select with onchange

Show the chosen option as soon as the user picks it.

HTML
<label for="flavor">Choose a flavor:</label>
<select id="flavor" name="flavor" onchange="showFlavor()">
  <option value="">Select one…</option>
  <option value="chocolate">Chocolate</option>
  <option value="vanilla">Vanilla</option>
  <option value="strawberry">Strawberry</option>
</select>
<p id="result">No flavor yet.</p>

<script>
  function showFlavor() {
    var sel = document.getElementById("flavor");
    document.getElementById("result").textContent =
      sel.value ? "You like " + sel.value + "." : "No flavor yet.";
  }
</script>
Try It Yourself

How It Works

  1. Label the control with for / id for accessibility.
  2. User picks an option — that commits the new value.
  3. change fires and onchange runs showFlavor().
  4. Read select.value and update the result text.

Example 2 — Text Input (Change on Blur)

Type a name, then leave the field — change runs after focus moves away.

HTML
<label for="name">Your name:</label>
<input type="text" id="name" name="name" onchange="showName()">
<p id="log">Waiting for change…</p>

<script>
  function showName() {
    document.getElementById("log").textContent =
      "Committed value: " + document.getElementById("name").value;
  }
</script>
Try It Yourself

How It Works

  1. User types in a text <input> — keystrokes alone usually do not fire change.
  2. Focus leaves the field (blur) after the value changed.
  3. change fires and the handler reads the committed value.
  4. For live updates while typing, use oninput instead.

Example 3 — onchange vs oninput

Same field, two listeners — see committed vs live updates.

HTML
<label for="q">Search:</label>
<input type="search" id="q" name="q">
<p id="inputLog">input: —</p>
<p id="changeLog">change: —</p>

<script>
  var q = document.getElementById("q");
  q.addEventListener("input", function () {
    document.getElementById("inputLog").textContent = "input: " + q.value;
  });
  q.addEventListener("change", function () {
    document.getElementById("changeLog").textContent = "change: " + q.value;
  });
</script>
Try It Yourself

How It Works

  1. input updates on each edit — good for live filters.
  2. change updates when the value is committed (for text, often on blur).
  3. Same control, different jobs — pick the event that matches your UX.
  4. addEventListener keeps both handlers clear and out of the markup.

Edge Cases & Pitfalls

Most confusion is timing: students expect every keystroke, or forget that script-set values may not fire change.

timing

Text waits for blur

For typing controls, change often fires only after focus leaves. Use oninput for per-keystroke reactions.

radio

Radio fires when checked

A radio fires change when it becomes checked — not when another radio in the group unchecks it.

script

Setting .value in JS

Changing a control’s value with script usually does not fire change. Dispatch an event yourself if your UI needs it.

vs blur

Not the same as onblur

onblur runs whenever focus leaves. onchange runs only if the value actually changed (for that commit).

property

.onchange = replaces listeners

Assigning the IDL property overwrites a previous assignment. Prefer addEventListener for multiple handlers.

Browser Support

onchange is an HTML event-handler attribute for form controls in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onchange

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the change event on common form controls.

98% Global support (approx.)
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 9+
Full support
Samsung Internet Modern · Full
Full support
Internet Explorer All
Supported
onchange Widely supported

Bottom line: Safe to use for committed form-value reactions. Prefer addEventListener in production, and use oninput when you need live updates while typing.

Key Takeaways

  • Role: run script when a form control’s value is committed.
  • vs oninput: live edits vs committed change.
  • Timing: select/checkbox soon; text often on blur after edit.
  • Apps: prefer addEventListener("change", …).

One line: onchange reacts when the user commits a new value — not on every keystroke.

Frequently Asked Questions

It is an event-handler attribute for form controls such as input, select, and textarea. When the user commits a change to the control’s value, the change event fires and the JavaScript in onchange runs.
Primarily input, select, and textarea. Timing depends on the control: checkboxes and radios fire on toggle/select; text fields often fire after the value changes and the control loses focus.
oninput usually fires on every edit (each keystroke). onchange fires when the change is committed — for text, often on blur; for select, when an option is chosen.
Usually not for every key. For text, search, email, and similar types (and textarea), change typically fires after the value has changed and the control loses focus.
Both work. For learning, onchange in HTML is fine. In production code, prefer element.addEventListener("change", handler) so you can attach multiple listeners and keep script out of markup.
No. The change event and onchange handler are standard HTML features and Baseline widely available since about July 2015.

Next: onclick

Run JavaScript when the user clicks (or activates) an element.

onclick tutorial →

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.

8 people found this page helpful