References

Beginner-friendly references for web development, with live, editable examples.

The HTML onchange event

Event All modern browsers Updated
Quick answer

The HTML onchange attribute runs JavaScript when the user commits a change to a form control (and the control loses focus, for text fields). It is an inline handler for the change event; in modern code prefer addEventListener('change', …).

Overview

The onchange event attribute runs JavaScript when a control's value is committed. In JavaScript the event itself is named change — drop the on prefix when you call addEventListener.

It is a form-related event, fired by form controls such as <input>, <select> and <textarea> (or the <form> itself) as the user interacts with them and as data is submitted or validated.

You can wire this up with the inline onchange HTML attribute, but the modern, recommended approach is element.addEventListener('change', handler) in JavaScript. That keeps behavior out of your markup, lets you attach several handlers to the same event, and makes them easy to remove. The inline attribute is fine for quick demos.

Syntax

<element onchange="handler()">…</element>

element.addEventListener('change', handler);

Example

Live example
<select onchange="this.nextElementSibling.textContent = 'You chose: ' + this.value"><option>A</option><option>B</option></select><span></span>

Best practices

  • Prefer element.addEventListener('change', handler) over the inline onchange attribute — it separates behavior from markup and allows multiple handlers.
  • Use native form validation (required, type, pattern) alongside JavaScript, not instead of it.
  • Re-validate on the server too — client-side events can be bypassed.
  • Give every control a <label> so the interaction is accessible.

Frequently asked questions

What is the onchange event?
It runs JavaScript when a control's value is committed. In JavaScript the event is named change.
What is the difference between oninput and onchange?
oninput fires on every keystroke as the value changes; onchange fires once the value is committed (often on blur).
Should I rely on form events for validation?
Use them for instant feedback, but always validate again on the server, since client-side checks can be bypassed.
Should I use the onchange attribute or addEventListener?
Prefer addEventListener('change', …) in JavaScript. The inline onchange attribute works but mixes behavior into the markup and allows only one handler per element.