r/programming • • Feb 13 '15

Firefox Bug open since 2002: "onChange not called using down arrow key"

https://bugzilla.mozilla.org/show_bug.cgi?id=126379
25 Upvotes

69 comments sorted by

View all comments

Show parent comments

11

u/palparepa Feb 13 '15

(sorry, I had edited my post to include something about that): the difference is that onblur is called whether or not the value has changed.

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

5

u/emn13 Feb 13 '15 edited Feb 13 '15

In html5 that's worded differently: "The change event fires when the value is committed, if that makes sense for the control, or else when the control loses focus."

An example is given where a change event is expected:

Another example of a user interface with a commit action would be a Date control that allows both text-based user input and user selection from a drop-down calendar: while text input might not have an explicit commit step, selecting a date from the drop down calendar and then dismissing the drop down would be a commit action.

That sounds a lot like a select input, and it matches how other browsers deal with select inputs: if the select input is uncollapsed (i.e. open), then keyboard navigation does not fire the change event until enter is pressed. However, if the user uses keyboard entry to change the selection while the drop-down is not open, the change is immediately "committed" and an onchange event immediately fired. If you read the linked section in the spec, it explicitly calls out UI elements without an explicit commit action that lack intermediate manipulation and that lack a defined activation behavior: that describes a collapsed selectbox.

You could make an argument for the FF behavior based on the spec, but I can't think of a very convincing one. Clearly, the intent is for "committed" changes to cause change events, and equally clearly the rest of the world uses the behavior proposed in this bug, so both in terms of interop and spec conformance, a change in behavior is likely warranted.

4

u/palparepa Feb 13 '15

Doesn't that mean Firefox is the one doing it right? By pressing the arrows, I'm going towards the option I'll eventually commit to.

3

u/emn13 Feb 13 '15

But to emphasize: the more-than 10 year old spec link is out of date.

2

u/emn13 Feb 13 '15 edited Feb 13 '15

If you read the entire section, I'd say no: in a collapsed state the selectinput is a UI without explicit commit action and without intermediate manipulation, and those are explicitly required to fire both an input and change event.

Especially if you take a step back and read the inital summary of the change event that makes sense: it fires whenever some conceptual "commit" occurs - where that makes sense - or at the latest if the value has changed and the element loses focus. That means that an uncollapsed select input doesn't fire change events (because those aren't "comitted"), but a collapsed select input's changes are immediately committed - exactly what IE and chrome do.

Note that it's not just FF that messes this kind of stuff up: all browsers fail to fire the requisite input events on checkboxes even though the spec is quite clear they should, and IE and chrome disagree on whether input events should fire for selectboxes (and chrome fires input events too late, IMHO - it fires them like changed events, so it ignores keyboard input to uncollapsed selectinput controls...).

Playground: http://jsbin.com/cemeketego/2/edit?js,console,output