I have a button and I want to perform an action if the user fires this button.
I remember being told that the click
event should not be used for this purpose because it only fires if the user, well, clicks the button and not if keyboard is used; BUT, in practice this does not seem to be the case:
document.getElementById('asdf').onclick = () => {
document.getElementById('log').appendChild(
document.createTextNode('click\n')
)
}
<button id="asdf">Press me</button>
<pre id="log"></pre>
It would seem that the spacebar fires the click event just fine!
This seems contrary to the MDN docs about click
, which say:
An element receives a
click
event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.
Is it, therefore, OK to use click
?
If it is not, which event should I use?
Should it be submit
?
If so, isn't submit
only related to the <form>
tag, not a <button>
tag as stated per the MDN?