[whatwg] Web Forms: change event

Garrett Smith dhtmlkitchen at gmail.com
Wed Oct 29 21:32:49 PDT 2008

Ian Hickson wrote:
> On Wed, 6 Jun 2007, Sander wrote:
>> The specs say that for <select> "...the change event shall be fired when 
>> the selection is completed...". Does this mean that, when using a 
>> keyboard for navigation, the change event is fired when the user hits 
>> 'enter' or when she navigates from one option to another? I assume it's 
>> the former, but I just to be sure as selectboxes with change event 
>> handlers are often (ab)used for navigation purposes. So, can anyone 
>> confirm my assumption? Thanks.
The HTML 4 specification[1] says:

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

A "test case"[2] I found does expects that onchange fires when the 
select has been changed and while it still has focus.

| Procedure
|   1. Using the keyboard or an assistive technology that emulates the
| keyboard, move focus to the select control.
|   2. Using the keyboard or an assistive technology that emulates the
| keyboard, select a value from the menu to trigger the onChange event.


| Expected results
|   1. A star colored according to the select option value appears when
| the onchange event is triggered.

Firing onchange before the select loses focus does not follow the HTML 
specification but is more useful and is what browsers mostly do.

Often, a select is used to trigger another action, such as showing more 
fields in a form. The user chooses an option and, based on the 
selection, a section of the form is shown. If the change event does not 
fire when the item is chosen, the callback will not be called and the 
user will not see the event until he changes focus by tabbing or 
clicking away.

Instead, the chagne event should (ideally) fire when a different option 
has been selected.

> Depends on the platform. 

Yep. Select-multiple adds one more variant.

> I've used different wording in HTML5, let me know 
> if it's confusing still (it probably is), and what it should say instead.

Link or text?

It would be useful to have x-browser way to get a select change event.

The change does not bubble in all browsers.

Depending on the implementation, a change event may or may not fire on:
  * alphanumeric key press (selects an element)
  * up/down arrow key press (selects an element)
  * select via enter after arrow key press

It is generally desirable to have a select fire the event when the value 
has changed, even if it still has focus.


comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >

More information about the whatwg mailing list