I have an element that is clickable and focusable but it is not a button.
I have used the tabindex="0" attribute so it can be tab focused.
For buttons, if you tab focus onto them and hit Enter/Space, it is effectively the same as clicking it.
I want to replicate the exact same behaviour with my element.
I assume there is a built-in way of doing this besides using a keydown event listener but I can't find a solution.
You can create a
keydownevent handler and dispatch a newclickevent if the enter key is being pressed.However, you won't get all event data as a natural click event. For example, the
e.clientXande.clientYproperties don't make much sense if you've used tab and enter. This likely won't matter to you though.