References

Beginner-friendly references for web development, with live, editable examples.

The HTML <button> type attribute

Attribute All modern browsers Updated
In short

The HTML type attribute sets a button's behavior. Use type="submit" to send a form, type="reset" to restore its fields or type="button" for a button that only runs JavaScript. Without a type, a button inside a form submits the form.

Overview

The HTML type attribute on a <button> sets what the button does when it is activated. It takes three values, submit to send the form, reset to restore the form's fields and button to do nothing by default.

When the attribute is missing or invalid, a button inside a form behaves as a submit button. That default explains the familiar bug where clicking a button that opens a menu or recalculates a total suddenly submits the form and reloads the page. Values like link, cancel or file don't exist and are treated as missing.

A type="button" element never submits, so clicking it never triggers form validation either. That makes it the right choice for any control that only runs JavaScript. Commands are an exception to the submit default. A typeless button with a command or commandfor attribute is not a submit button, and inside a form it does nothing at all.

Only submit buttons honor formaction, formmethod, formenctype, formtarget and formnovalidate. The reset type is rarely a good idea, because one click puts every field back to its initial value and throws away what the user typed.

Syntax

<button type="button">Show details</button>

Values

Value
submit (default in a form) | reset | button

Example

Live example
<form id="order">
  <label for="qty">Quantity</label>
  <input type="text" id="qty" name="qty" value="1">
  <button type="button" id="calc">Recalculate</button>
  <button type="reset">Reset</button>
  <button type="submit">Order</button>
</form>
<p id="log">Change the quantity, then try each button.</p>
<script>
function log(msg) {
  document.getElementById('log').textContent = msg;
}
document.getElementById('calc').addEventListener('click', function () {
  log('type="button" ran JavaScript and did not submit.');
});
document.getElementById('order').addEventListener('submit', function (e) {
  e.preventDefault();
  log('type="submit" submitted the form.');
});
document.getElementById('order').addEventListener('reset', function () {
  log('type="reset" restored the initial value.');
});
</script>

Best practices

  • Write the type on every button, including submit buttons, so the intent is clear from the markup.
  • Use type="button" for toggles, menus, tabs and other controls that only run JavaScript.
  • Give buttons with command or commandfor a type="button" when they sit inside a form.
  • Use an <a> element for navigation instead of a button whose click handler changes the page location.
  • Leave out reset buttons unless users truly need them, and never place one where it can be hit instead of Submit.

Accessibility

The type attribute changes behavior, not the role. ARIA in HTML gives every <button> the implicit role button, whatever its type. Make the visible label say what will happen, such as Place order rather than Submit.

Frequently asked questions

What is the default type of an HTML button?
The default button type is submit whenever the button sits inside a form. An invalid value such as type=link is treated the same way.
Why does my button submit the form when I click it?
A missing type attribute makes a button submit its form. Add type=button to buttons that should only run JavaScript.
What is the difference between type=button and type=submit?
Of the two, only type=submit sends the form. A type=button does nothing until your script handles the click, and clicking it never triggers form validation.
Is there a type=link or type=cancel for buttons?
No, the button type attribute only accepts submit, reset and button. Use an a element for navigation, and commandfor with command=close or formmethod=dialog to close a dialog.
Should I use type=reset on a button?
A type=reset button is rarely worth including. One click returns every field to its initial value, and MDN notes that this behavior tends to annoy users.
Why does pressing Enter submit a form whose buttons are all type=button?
A form with one text field submits on Enter even when every button has type=button. The HTML standard allows this implicit submission when a form has no submit button, so call preventDefault() in a submit handler if you need to stop it.