onClick vs onSubmit

Nishit Ranjan
1 min readAug 30, 2020

onClick captures mouse clicks. So, if you click a button on your HTML, you can add a handler to onClick.

When your HTML is a form with a number of fields(e.g name, age etc), you shall add onSubmit handler to your form. Even if you have a submit, adding onSumit to the form element makes more sense than an onClick event on the submit button of the form.

The main reason being that your user can then have the option to trigger form submission using both click and keyboard events like keypress. It’s a better user experience as the user gets an additional option and is not tied to use clicks. Also, you won’t miss on any form submission that is triggered via keyboard event(e.g. a keypress on the Enter key).

--

--

Nishit Ranjan
0 Followers

I am a software engineer. I specialize in JavaScript, React.js, NodeJS, Docker and Kubernetes.