Home  >  Article  >  Web Front-end  >  How to Create a Button Outside a Form That Doesn\'t Submit It?

How to Create a Button Outside a Form That Doesn\'t Submit It?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-26 15:59:02961browse

How to Create a Button Outside a Form That Doesn't Submit It?

HTML Button to Resist Form Submission

Question:
How can one create a button outside an HTML form that does not submit the form when clicked?

Consider the following code:

<code class="html"><form id="myform">
  <label>Label
    <input />
  </label>
</form>
<button>My Button</button></code>

Despite the absence of a form attribute in the button, it still submits the form.

Answer:
To prevent the button from submitting the form, it should be declared as a "button" type. Here's the updated code:

<code class="html"><button type="button">My Button</button></code>

Reason:
HTML buttons inherit the Submit Button state by default. By explicitly setting the type as "button," we override the default and prevent form submission.

The above is the detailed content of How to Create a Button Outside a Form That Doesn\'t Submit It?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn