Categories
JavaScript Answers

How to stop form submission with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *