Form onsubmit versus submit button onclick Form onsubmit versus submit button onclick javascript javascript

Form onsubmit versus submit button onclick


Form onsubmit is a more correct approach, for the simple reason that a form can also be submitted with the <ENTER> key, and not just by clicking the submit button.


There are no direct reasons to use a from’s onsubmit handler over a button’s onclick handler. One should use named events according to their intent.

You may consider a scenario with two actions – a “send” action (as in “to send an e-mail”) and a “save” (as in “draft”). The former action assumes the form filled in a coherent manner, while the latter action should allow the user to persist whatever state the user likes. That makes the click handlers ideal places to decide on validation and cancellation. The relevance of the submit event in this case is undetermined.

To make the matter even more complicated you may add auto-save and optimistic concurrency to the scenario.

IMHO The years of tries and errors in the matter resulted in the HTML5 standard to have ruled the question in favor of the button element. See button’s [form*] attributes that allow to change the form action, method by each button individually.