Buttons and Submit Behavior
How button types submit, reset or do nothing, why a button inside a form submits by default, and how formaction, form and formnovalidate change a button's job.
Buttons and Submit Behavior
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A `<button>` inside a form submits it by default, even with no `type`. In a test, a button with no type, with an invalid type such as `bogus`, and an `<input type="submit">` each submitted the form.
- `type="button"` does nothing on its own. Use it for buttons that run JavaScript or open a menu inside a form, so they do not submit by accident.
- `type="reset"` restores each field to the value in its HTML, not to empty. In a test, a field with `value="start"` was back to `start` after a reset.
- A button can carry data and change the destination. Its `name` and `value` are submitted only when it was the one clicked, `formaction` and `formmethod` override the form's, and `form="id"` attaches a button that sits outside the form.
- `formnovalidate` on a submit button skips built-in validation for that click, which is how a "Save draft" button works next to a "Submit" button.
The button that submitted the form
A common first bug in forms is a button that was meant to do something small, like toggle a password’s visibility, and instead reloads the page. The cause is a default most people do not know about. A <button> inside a <form> is a submit button unless you say otherwise.
Buttons are small, but they are where a form’s behavior is decided. This lesson covers the three button types, what each does, and the attributes that let a button change where and how a form submits.
The three types
<button type="submit">Send</button>
<button type="reset">Clear</button>
<button type="button">Show password</button>
submitruns the form’s submission: validate, then send.resetputs every field back to its starting value.buttonhas no built-in behavior. It exists to be wired up with JavaScript.
If you leave type out, you get submit. An invalid value also gives submit. I tested a form with a plain <button>, a <button type="bogus">, a <button type="button"> and an <input type="submit">. In headless Chromium, Firefox and WebKit, the first, second and fourth each fired a submit event and the type="button" one did not; the type property read submit, button and submit for the first three buttons. The rule of thumb is simple: write type on every button, and when it is not meant to submit, write type="button".
What reset really does
Reset does not clear. It restores. In a test, a text input with value="start" was changed to something else, and after a reset it held start again. A checkbox with checked in the HTML came back checked, and a select returned to the option marked selected. A textarea went back to the text between its tags.
That surprises users who expect an empty form, and it costs them work if they click it by mistake. Most forms have no good reason for a reset button.
<button> or <input type="submit">
Both submit. <input type="submit" value="Send"> is a self-closing element whose label is the value attribute, so it can only contain text. <button> can contain markup, such as an icon and text, and it is easier to style. Prefer <button>.
Buttons that carry data and override the form
A button’s name and value are submitted only if that button triggered the submission. In a test with two submit buttons, name="act" with value="save" and value="draft", clicking each sent act=save and act=draft. The server reads that to decide what to do. In JavaScript, the submit event’s submitter property gives the button that was used: it was save and draft in the test.
A submit button can also change the destination. formaction, formmethod, formtarget, formenctype and formnovalidate override the form’s own action, method, target, enctype and novalidate for that click. In the same test, a button with formaction="/draft" and formmethod="get" sent GET /draft?q=1&act=draft while the other sent POST /a, in all three browsers. formnovalidate is how a “Save draft” button skips validation that the “Submit” button must pass.
A button does not need to be inside the form. Give the form an id and put form="id" on the button. In a test, a button placed after the form’s closing tag submitted it.
Enter and implicit submission
Pressing Enter in a text field presses the form’s default button, the first submit button in the form. In a test, a form with a submit button submitted on Enter. A form with a single text field and no button also submitted. A form with two text fields and no submit button, or only a type="button" button, did not. If you build a form out of two fields and a button you wrote as type="button", Enter will not work, so keyboard users will be unable to submit.
A related trap is validation. Submitting through a button runs the form’s built-in validation, and a failed check stops the submit event from firing. In a test, a required empty field made a submit button’s click fire no submit event, while a formnovalidate button did fire one. Built-in form validation covers the details.
A button is not a link, and not a div
Use <button> for actions, and <a href> for going somewhere. A <div onclick> gets neither keyboard focus nor Enter and Space activation, nor the button role. If you need something that looks like a link but acts like a button, style a <button>. The keyboard and focus lesson explains why.
What the video covers
The embed is the whole of Leela Web Dev’s “43. Submit vs Reset vs Button in forms. Difference between input button and button element” (May 2022), one lesson from a long HTML course. Its description says it covers the buttons available in input elements and the difference between the input button and the <button> element. It has no chapters. The presenter speaks Indian-accented English, and the channel’s auto-generated captions are English; I could not confirm the audio by ear, so check it before trusting this pick. I could not read a transcript. The video is nearly fifteen minutes, long for its scope, and nothing in its description mentions formaction, form or formnovalidate, so the data, override and implicit-submission sections above are the lesson’s own and were tested in headless Chromium, Firefox and WebKit. The runner-up is under three minutes and only separates button from submit.
Where this fits
This follows fieldset, legend and grouping. Next, built-in form validation covers what the browser checks when a submit button is pressed.
Check yourself
You add a "Show password" `<button>` next to a password field inside a form. Clicking it submits the form. Why, and what is the fix?
A `<button>` with no `type` defaults to `type="submit"`. Add `type="button"` so the click does only what your script says.
What does `<button type="reset">` do to a field that had `value="start"` in the HTML and now holds something else?
It puts the value back to `start`, the value from the markup, not to an empty string. Reset buttons also clear checked boxes back to their default state. Because users click them by accident and lose their work, they are rarely worth including.
A form has two submit buttons, `name="act" value="save"` and `name="act" value="draft"`. What does the server receive when the second one is clicked?
`act=draft`. Only the button that triggered the submission contributes its name and value. In a test, clicking each one produced `act=save` and `act=draft`, and the submit event's `submitter` property pointed at the same button.
How can a button outside a form submit it?
Give the form an `id` and put `form="that-id"` on the button. In a test, a `<button form="f">` placed after the form's closing tag submitted it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.