4

I was slightly surprised to find out that the onChange event in an html document is fired on a text input or textarea not at the very moment when its value actually changes, but only when focus leaves the element (if its value has changed, of course).

So I was looking for the specification that states that, and I can't find it. I can find millions of tutorials explaining that, including W3Schools' ones, but I can't find the standard that defines when the event is expected to be fired.

In the HTML5 specification itself, the event's name is listed but nothing is said about it: http://www.w3.org/html/wg/drafts/html/master/

In this other spec, "DOM level 3 Events Specification", it is not even mentioned: http://www.w3.org/TR/DOM-Level-3-Events/

So what is the standard that defines it?

matteo
  • 2,934
  • 7
  • 44
  • 59

1 Answers1

3

It's briefly mentioned in the Intrinsic events section of the W3C 4.01 specification:

"The onchange event occurs when a control loses the input focus and its value has been modified since gaining focus".

It's also mentioned a little more extensively on MSDN:

"This event is fired when the contents are committed and not while the value is changing. For example, on a text box, this event is not fired while the user is typing, but rather when the user commits the change by leaving the text box that has focus. In addition, this event is executed before the code specified by onblur when the control is also losing the focus."

Finally, on the MDN:

Depending on the kind of the form element being changed and the way the user interacts with the element, the change event fires at a different moment:

  • When the element is activated (by clicking or using the keyboard) for <input type="radio"> and <input type="checkbox">;
  • When the user commits the change explicitly (e.g. by selecting a value from a <select>'s dropdown with a mouse click, by selecting a date from a date pickier for <input type="date">, by selecting a file in the file picker for <input type="file">, etc.);
  • When the element loses focus after its value was changed, but not committed (e.g. after editing the value of <textarea> or <input type="text">).

Another potentially useful link - WhatWg - specification - change event.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • 1
    Thanks. The WhatWg one seems to be the closest thing to a standard that exists right now. (It's insanely convolute, though). I almost missed it :) (I was going to comment I found it linked from in the MDN one). Interesting that W3C decided to completely drop a piece of specification in HTML5 which was pretty clear in HTML4, though incomplete and partly untrue (or no more true: what stated there doesn't apply to checkboxes and radios) instead of completing and improving it. – matteo Jun 27 '13 at 11:00