Firefox, when pressing Space or Enter and creating a new button and focusing it, issues a click event on it
Categories
(Core :: DOM: UI Events & Focus Handling, defect, P2)
Tracking
()
People
(Reporter: benjamin.jentsch, Unassigned)
Details
User Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0
Steps to reproduce:
- Created a 'keydown' listener on document.body
- Limited it to Space and Enter keys (keyCode 32 + 13)
- In the keydown listener, created a button element
- After appending it to the DOM, focused the button element
Actual results:
The button is created and focused, and a click event is recorded with the button as its target.
See: https://codepen.io/franklin35/pen/mddGyKB?editors=0010
Expected results:
On Space or Enter keydown, a button element should be appended to the DOM and be focused.
No click event should have fired on the button element after it receiving focus.
For "Enter" the behavior is the same on Chrome, it could be fixed by using the 'keyup' listener. Benjamin, does that already solve your problem?
Reporter | ||
Comment 2•6 years ago
|
||
You're right as far as "Enter" is concerned, I failed to re-check that against my sample.
The issue with "Space" persists (and for my use case, it's no alternative switching to Enter, sadly).
It should work for "Space" as well. Note, the example doesn't work for multiple "Space" or "Enter" key presses, because after the first press, a button is focused.
If there are no other concerns about this functionality, can we close those this issue?
Reporter | ||
Comment 5•6 years ago
|
||
I have updated the example with a Space-only listener: https://codepen.io/franklin35/pen/VwwGjYq
You don't have to press Space multiple times. At the first press, the newly created button is triggered. This shouldn't be happening because the button is created after the keydown event fired.
Even calling stopPropagation on the event won't stop the click event from occuring.
To me, this is still viable as a bug and should be fixed, unless this is in any way wanted and/or specified behaviour.
The updated example isn't using the "keyup" event.
Reporter | ||
Comment 7•6 years ago
|
||
Yes, the problem is "keydown"-specific.
Listening to "keyup", I get nearly the same functionality, except for one thing: preventDefault() on "keyup" does not prevent scrolling from happening when pressing Space.
Updated•3 years ago
|
Description
•