When using the button tag, does the type attribute have to be defined, or is it semantic to just have?
<button>Click Me</button>
When using the button tag, does the type attribute have to be defined, or is it semantic to just have?
<button>Click Me</button>
No, you don't have to specify it, it defaults to the value
submit
.See the HTML 4.x specification:
Compare with the action attribute for forms where it says #REQUIRED instead of giving a default value.
As @Quentin’s answer explains, the
type
attribute is not required and it defaults tosubmit
. There is no change to this in HTML5. However, the situation is slightly more complicated.If the element appears outside any
form
element, the above still applies, but there is no form to submit. HTML5 clarifies this by describing the functionality so that it becomes clear that if there is no “form owner” (either an enclosingform
element or aform
element explicitly associated with thebutton
element with an HTML5 attribute), there is no action – except as programmed with scripting, of course.In effect, this means that outside a
form
element, abutton
element defaults totype=button
functionally. This implies that if abutton
element that hastype
defaulted changes its context (e.g., gets wrapped inside aform
element), its functionality changes. Therefore, for clarity and safety, it is better to explicitly specify thetype
attribute, e.g.<button type=button>
or<button type=submit>
.