Search code examples
javascriptfocusdom-eventskeyevent

Do browser key events fire (bubble) only to elements in focus?


Experimental evidence has led me to believe that key (up, down, press) events fire (in bubble phase) only to elements in focus.

This behavior may be intuitive, obvious and/or desirable, but haven't seen this documented anywhere, so I was hoping the community would confirm my "theory".

Otherwise, I have some code that's not properly letting these events bubble up.


Solution

  • key events are always targeted to the element that has focus, and bubble up until cancelled.

    You cannot press a key without an input element or the document/window itself being the target, and having the focus.

    If you are looking at the parent of an input, and the focus changes to the window, you cannot find the next key event at the parent (because it isn't there), but you always find it at the document level.