A <button> element without an explicit type attribute defaults to type="submit" per the HTML Living
Standard, but this default has not always been implemented consistently across browsers. When the button is inside a <form>, an
unintended submit type causes the form to be submitted on click, even if that was not the developer’s intent. The HTML specification
defines three valid type values for buttons: submit, reset, and button. Always setting the type
attribute explicitly removes any ambiguity and makes each button’s purpose clear regardless of its context or the browser rendering it.
The rule also flags buttons with an invalid type value. When the value is not one of the three defined values, the browser falls back
to its default behavior for buttons without a type, which creates the same hazard.
function SearchForm() {
return (
<form action="/search">
<input type="text" name="q" />
<button>Search</button> // Noncompliant: defaults to type="submit"
<button onClick={handleClear}>Clear</button> // Noncompliant: defaults to type="submit"
</form>
);
}
function SearchForm() {
return (
<form action="/search">
<input type="text" name="q" />
<button type="submit">Search</button>
<button type="button" onClick={handleClear}>Clear</button>
</form>
);
}
<button type="action">Save</button> <!-- Noncompliant: "action" is not a valid type -->
<button type="submit">Save</button>
<button> element -
type attributebutton element -
type