1

Naively you might assume that the event is triggered that when the (a?) input button of type "submit" is clicked.

In fact, as far as I'm able to ascertain, every button within a form triggers the event.

Is there some way to set a button to not trigger the event?

kesoper
  • 35
  • 4
  • The simpliest way is to use a button `` – Cid Feb 18 '20 at 11:20
  • 1
    Does this answer your question? [HTML button to NOT submit form](https://stackoverflow.com/questions/2825856/html-button-to-not-submit-form) – caramba Feb 18 '20 at 11:21

2 Answers2

3

every button within a form triggers the event

No. Only submit buttons do. (As well as a few other cases such as when the Enter key is pressed in a text input).

Note that <button> elements are type="submit" by default and should be type="button" if you want a non-submit button for handing JS from.

You can also attach a JavaScript click event listener to a button and call preventDefault on the event object.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Button's default type is submit. If you want buttons that don't submit, you have to change their type to button.

jorbuedo
  • 2,041
  • 1
  • 8
  • 20