Sometimes, we want to cancel form submission in submit button onclick event with JavaScript.
In this article, we’ll look at how to cancel form submission in submit button onclick event with JavaScript.
How to cancel form submission in submit button onclick event with JavaScript?
To cancel form submission in submit button onclick event with JavaScript, we return false
if we don’t want to sumbmit the form in the form submit handler.
For instance, we write
document.getElementById("my-form").onsubmit = () => {
return isValidForm();
};
to select the form with getElementById
.
Then we set its onsubmit
property to a function that returns the return value of the isValidForm
function.
If isValidForm
returns false
, then the form won’t be submitted.
Conclusion
To cancel form submission in submit button onclick event with JavaScript, we return false
if we don’t want to sumbmit the form in the form submit handler.