useButtonType (HTML)
Summary
Section titled “Summary”- Rule available since:
v2.4.0 - Diagnostic Category:
lint/a11y/useButtonType - This rule is recommended, meaning it is enabled by default.
- This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Inspired from
react/button-has-type - Inspired from
react-dom/no-missing-button-type - Inspired from
@eslint-react/dom-no-missing-button-type - Same as
svelte/button-has-type - Same as
vue/html-button-has-type - Same as
@html-eslint/require-button-type
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "a11y": { "useButtonType": "error" } } }}Description
Section titled “Description”Require an explicit, valid type on every <button>.
A button without a type defaults to submit, which can submit a surrounding form
unexpectedly. Use button, submit, or reset to state the intended behavior.
A button element without a type attribute
behaves as type="submit".
Inside a form, clicking it submits the form, which often reloads the page.
That is rarely the intent of a button that opens a menu, toggles a panel, or runs a click handler.
This is easy to miss in Vue, Svelte, and Astro components, because a button rendered by one component
can end up inside a form rendered by another.
An explicit type makes the button behave the same wherever it’s rendered.
The HTML specification defines three values for type:
button: the button has no default behavior. Use it for buttons handled by JavaScript.submit: the button submits its form.reset: the button resets the controls of its form to their initial values.
Browsers treat any other value the same as a missing type, so the rule reports invalid values too.
In Vue, Svelte, and Astro files, the rule ignores type values bound to an expression,
such as :type="buttonType" or type={buttonType}, because their value can’t be known statically.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<button>Do something</button>code-block.html:1:1 lint/a11y/useButtonType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Provide an explicit type attribute for the button element.
> 1 │ <button>Do something</button>
│ ^^^^^^^^
2 │
ℹ The default type of a button is submit, which causes the submission of a form when placed inside a `form` element.
ℹ Allowed button types are: submit, button or reset
<button type="incorrectType">Do something</button>code-block.html:1:1 lint/a11y/useButtonType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Provide a valid type attribute for the button element.
> 1 │ <button type="incorrectType">Do something</button>
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ The default type of a button is submit, which causes the submission of a form when placed inside a `form` element.
ℹ Allowed button types are: submit, button or reset
Clicking this button submits the form instead of clearing it:
<form action="/search"> <input name="query" /> <button>Clear</button></form>code-block.html:3:5 lint/a11y/useButtonType ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Provide an explicit type attribute for the button element.
1 │ <form action="/search">
2 │ <input name="query" />
> 3 │ <button>Clear</button>
│ ^^^^^^^^
4 │ </form>
5 │
ℹ The default type of a button is submit, which causes the submission of a form when placed inside a `form` element.
ℹ Allowed button types are: submit, button or reset
<button type="button">Do something</button>
<form action="/search"> <input name="query" /> <button type="reset">Clear</button> <button type="submit">Search</button></form>Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.