Specify Button Type Attribute

ID

html.wrong_type_attribute

Severity

low

Remediation Complexity

low

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Html

Tags

reliability

Description

Flags a <button> element that has no explicit type attribute.

Rationale

When a <button> omits type, the HTML default is submit. Inside a form that means an otherwise innocent-looking button will submit the form when clicked, or when the user presses Enter, which is a frequent source of accidental submissions and confusing behaviour. Declaring the intended type makes the element’s role explicit.

<button>Go</button>                <!-- FLAW — defaults to submit -->

<button type="button">Go</button>  <!-- OK — role is explicit -->

Remediation

Add an explicit type attribute to every <button>: type="button" for plain actions, type="submit" for the form-submit button, or type="reset" to reset the form.