Sub topic 4
Buttons and how a form is submitted
This chapter assumes you know basic HTML structure and have written a simple <form> with a few inputs. It explains how buttons trigger submission, what the browser does next, and the mistakes that cause forms to submit when you don't expect them to.
The three button types
The <button> element has a type attribute with three values.
| Type | Behavior |
|---|---|
| `submit` | Validates the form, then submits it. This is the default. |
| `reset` | Restores every control to its initial value. |
| `button` | Does nothing by itself. Use it for JavaScript actions. |
The default is the source of a common bug. A <button> inside a form with no type attribute is a submit button. If you add a "Show password" or "Add another row" button and forget type="button", clicking it submits the form.
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>
<button type="button" id="help">What is this for?</button>
<button type="submit">Subscribe</button>
</form>Here the first button is inert until you attach a click handler. The second one submits.
Other ways to submit
A form can be submitted without clicking a submit button:
- Pressing Enter in a text field submits the form through its default button, which is the first submit button in tree order. If the form has no submit button, most browsers submit on Enter only when the form has a single text field.
- Calling
form.requestSubmit()in JavaScript runs validation and fires thesubmitevent, like a real click. - Calling
form.submit()skips validation and does not fire thesubmitevent. Avoid it unless you want that behavior.
What happens on submit
When a user activates a submit button, the browser follows a fixed sequence.
Two details matter. If validation fails, the submit event never fires, so your handler does not run. And unless you call preventDefault(), the browser navigates to the response, which replaces the current page.
Buttons can override the form
A submit button can carry its own settings. The form* attributes replace the form's values for that one submission.
<form action="/articles" method="post">
<label for="title">Title</label>
<input id="title" name="title" required>
<button type="submit">Publish</button>
<button type="submit" formaction="/drafts" formnovalidate>Save draft</button>
</form>"Save draft" sends the data to /drafts and skips validation, so an incomplete title is accepted. The attributes formmethod, formenctype, and formtarget work the same way.
A button with a name and value also adds that pair to the submitted data, but only the button that was used. This lets the server tell which button was clicked.
Preventing double submission
Users often click twice. A common fix is to disable the button inside the submit handler. Do this in the handler, not in a click handler. Disabling the button on click can cancel the submission in some browsers. Also, a disabled button's name and value are not sent.
const form = document.querySelector('form');
form.addEventListener('submit', () => {
form.querySelector('button[type="submit"]').disabled = true;
});If you call preventDefault() and the request fails, re-enable the button so the user can retry.
Exercise
A form has a text input and a "Preview" button written as <button id="preview">Preview</button>. Clicking Preview sends the form to the server. What is the cause, and what is the fix?
Show answer
A <button> with no type defaults to type="submit". Change it to <button type="button" id="preview">Preview</button>, then attach a click handler for the preview behavior.
Exercise
Your submit event handler never runs when a required field is empty. Is this a bug?
Show answer
No. Built-in validation runs first. When a control is invalid, the browser shows a message and does not fire the submit event. The handler runs only after validation passes.