HTML5 Beginner Accessible Forms · 6 min read

HTML Button Types: submit, button, and reset

Lesson 13 of 36 in the HTML5 learning path.

Why does a button submit the form when I only wanted a click?

The button element submits its form when type is submit, and also when type is omitted. That default is why a control you added for a preview or a calculation suddenly navigates away. type="button" performs no submit. It is the right type for a click that should stay on the page and run script. type="reset" restores every control to the value it had when the document was loaded. On the lab note, that initial value is Lab report 2, so Clear puts those words back. It does not mean blank.

Set type on every button. Use submit for the action that should send the form, button for Preview, and reset only when restoring the initial values is truly the request. Many forms should not offer reset, because it is easy to hit by mistake. The lab note page shows all three so the difference is visible in one form.

Syntax & Parameters

Syntax
<button type="submit">Save</button> <button type="button">Preview</button> <button type="reset">Clear</button>

Example

Interactive preview available
form.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Lab note buttons</title>
  <style>
    body { margin: 0; font: 1.05rem/1.55 Georgia, "Times New Roman", serif; color: #1c1917; background: #faf7f2; }
    header { background: #1e3a5f; color: #f8fafc; padding: 1.25rem 1.5rem; }
    main { max-width: 42rem; margin: 0 auto; padding: 1.25rem 1.5rem 2rem; }
    h1 { font-size: 2rem; line-height: 1.2; margin: 0 0 0.75rem; }
    p { margin: 0 0 0.85rem; }
    label { display: block; font-weight: 600; margin: 0.8rem 0 0.3rem; }
    input { width: 100%; max-width: 24rem; box-sizing: border-box; padding: 0.45rem 0.55rem; font: inherit; }
    button { font: inherit; margin: 0.8rem 0.4rem 0 0; padding: 0.4rem 0.75rem; }
  </style>
</head>
<body>
  <header>
    <p>North Campus, Web Systems 1</p>
  </header>
  <main>
    <h1>Lab note</h1>
    <p>Inside a form, a button submits unless its type is button or reset. Preview must not submit. Clear restores the initial value, which is Lab report 2, rather than wiping the field to blank.</p>
    <form id="note-form" method="get" action="/labs/note">
      <label for="note">Note to the instructor</label>
      <input id="note" name="note" type="text" value="Lab report 2">
      <button type="submit">Save note</button>
      <!-- type="button" keeps this control from submitting the form. -->
      <button type="button" id="preview">Preview</button>
      <button type="reset">Clear</button>
    </form>
    <p id="preview-line" hidden></p>
  </main>
  <script>
    document.getElementById("preview").addEventListener("click", function () {
      var line = document.getElementById("preview-line");
      var value = document.getElementById("note").value;
      line.hidden = false;
      line.textContent = "Preview only: " + value;
    });
  </script>
</body>
</html>

Expected Output

The page shows a note field containing Lab report 2 and three buttons: Save note, Preview, and Clear. Choosing Preview reveals the line Preview only: followed by the current field text. The address bar does not change, and no request is sent to /labs/note.

Choosing Clear after the text was edited puts Lab report 2 back into the field. Choosing Save note navigates to /labs/note?note=Lab+report+2 when the field still has its initial text, or with the edited text if it was changed first. A button without a type, placed in this form, would take that same navigation path. Preview does not, because its type is button.

Line-by-Line Walkthrough

  1. The form uses method="get" and action="/labs/note", so a real submit puts the note in the query string of that path.
  2. The input has value="Lab report 2". That string is the initial value reset will restore, not a hint and not a one-time default that disappears.
  3. Save note is type="submit". Activating it is the same as submitting the form.
  4. Preview is type="button". The comment marks why. Without that type, the button would submit because it sits inside the form.
  5. The click listener on Preview reads the current input value and writes it into the preview paragraph. It does not call submit.
  6. The preview paragraph starts hidden and is shown only after Preview runs, so a submit is not required to see the line.
  7. Clear is type="reset". It restores Lab report 2 even if the student deleted or replaced the text.
  8. Reset does not run the preview script and does not navigate. It only restores controls in this form.
  9. The opener is not an anchor. Preview stays on the page, which is the behavior people expect from a button that is not a link.

Edit the note, choose Preview, and confirm the URL is unchanged. Choose Clear and confirm the field returns to Lab report 2. Choose Save note and confirm the browser requests /labs/note with the note in the query string.

Common Pitfalls & Security

The default type is the bug. A button added for a harmless click submits the form, and a GET submit can put a draft note into the URL, the history, and the server logs. Set the type before you wire the script.

  • Omitting type on a button inside a form makes it type="submit". The click navigates or posts even if you also added a listener.
  • type="reset" restores the initial value attribute. If that attribute is Lab report 2, Clear will not leave the field empty.
  • A reset control next to submit is easy to hit by accident and throws away what the student just typed.
  • Using an anchor styled as a button for Preview creates a navigation. Use a button when there is no new URL.
  • Preventing the default submit in script, while leaving the type as submit, still submits when script fails to load. Set type="button" in the HTML.

Best Practices & Modern Standards

HTML5 gives a button three types, and the safe habit is to write one of them every time. submit sends the form. button runs script and stays put. reset restores initial values, and you should include it only when that restore is a deliberate action the label makes obvious.

Keep the submit button as a real submit control so Enter in the field still works. Do not replace it with a click listener that calls submit unless you have a reason. Name the buttons as verbs: Save note, Preview, Clear. A label of OK does not say which of the three types the student is about to activate.

Real-World Use

The lab note form on the course site is this trio. Students preview the sentence that will be stored with the lab, without leaving the page, then save it when the sentence is right. Clear is there because the field starts with Lab report 2 and some students want that starter text back after an experiment.

The same default bites a checkout page when Add gift wrap is a button with no type. One click submits the order. Setting type="button" on every secondary control, and type="submit" only on Place order, is the fix the department style guide now requires.

Frequently Asked Questions

What is the default type of a button in a form?

The default is submit. A button with no type attribute inside a form submits that form. Outside a form, a button does not submit anything because there is no form to send. Write the type anyway so the intent stays obvious when the markup is moved.

Does type=button still need a click listener?

Yes, if you want it to do work. type="button" only means the click will not submit the form. The Preview script in the sample is what writes the preview line. Without that listener the button would focus and do nothing else.

Why did Clear put the old sentence back?

Reset restores the initial value from the value attribute, which is Lab report 2. It does not clear the attribute, and it does not mean an empty string unless the field started empty. If you want a control that blanks the field, write a type="button" script that sets value to an empty string.

Will Enter in the text field submit the form?

Yes, in a form with one text field, Enter activates the submit button, and that path is independent of Preview. Leave Save note as type="submit" so Enter and the button agree. Preview stays type="button" so it is not the Enter action.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…