Home >Web Front-end >JS Tutorial >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!