Languages
[Edit]
EN

HTML - prevent button from submitting form

0 points
Created by:
Dexter
660

In this article, we would like to show you how to prevent the button from submitting the form in HTML.

Quick solution:

<button type="button">Click me</button>

Note:

To prevent the button from submitting the form set button type to "button"  since its default type is "submit".

 

Practical example

In this example, we set the button element type to "button" to prevent it from submitting the form.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <form>
    <label>
      <span>input:</span>
      <input type="text" name="field-1" />
    </label>
    <button type="button">Click me</button>
  </form>
</body>
</html>

Alternative solutions

You can also prevent form from being submitted using preventDefault() method.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <form onsubmit="event.preventDefault()">
    <input type="text" />
    <input type="submit" value="Submit" />
  </form>
</body>
</html>

or by returning false on submit event:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <form onsubmit="return false;">
    <input type="text" />
    <input type="submit" value="Submit" />
  </form>
</body>
</html>

 

References

  1. <button>: The Button element - HTML: HyperText Markup Language | MDN

Alternative titles

  1. HTML - prevent button from form submit
  2. HTML - prevent input type submit from submitting form
  3. HTML - prevent submit button from submitting form
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join