[whatwg] focus change inside keypress event handler

Scott González scott.gonzalez at gmail.com
Thu Oct 29 08:03:04 PDT 2009

On Thu, Oct 29, 2009 at 9:20 AM, Michael A. Puls II <shadow2531 at gmail.com>wrote:

> Safari and Firefox will allow focus() inside the handler to decide where
> the character gets inserted, but only with "keydown". With "keypress" (and
> textInput in webkit) in Firefox and Safari, it appears that the char was
> already inserted right after keydown, so a focus() change is already too
> late. Despite that though, preventDefault() still works in Firefox and
> Safari inside a "keypress" handler to prevent the char from being inserted.
> So, I'm not exactly sure what's they're doing behind the scenes.

DOM Level 3 Events says "The default action of the keypress event depends
upon the key and the context: if the key is associated with a character, and
if there currently focused element in the document can receive text (such as
a form input or an editable text block), the default action is to dispatch a
textInput event with the character as the value of the TextEvent.data

The default action of a keypress is not to insert a character in the element
that has focus, but to insert a character on the element that represents the
context of the keypress. In the given example, the keypress's context is the
first field, not the second. I agree though that the documentation could be
clearer about this as it's not explicitly stated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/attachments/20091029/8d87496e/attachment-0001.htm>

More information about the whatwg mailing list