To stop form submission with JavaScript, we call the preventDefault
method.
For instance, we write
<form onsubmit="submitForm(event)">
<input type="text" />
<input type="submit" />
</form>
to add a form.
We set its onsubmit
attribute to call the submitForm
function with the submit event
object.
Then we write
function submitForm(event) {
event.preventDefault();
}
to define the submitForm
function.
In it, we call event.preventDefault
to stop form submissmision when we click on the submit button in the form.