Home > Web Front-end > JS Tutorial > body text

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

Mary-Kate Olsen
Release: 2024-10-26 15:59:02
Original
1026 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template