0

When using the button tag, does the type attribute have to be defined, or is it semantic to just have?

<button>Click Me</button> 

2 Answers 2

2

No, you don't have to specify it, it defaults to the value submit.

See the HTML 4.x specification:

type (button|submit|reset) submit -- for use as form button -- ^^^^^^ default value 

Compare with the action attribute for forms where it says #REQUIRED instead of giving a default value.

action %URI; #REQUIRED -- server-side form handler -- 
Sign up to request clarification or add additional context in comments.

2 Comments

What would be considered the most semantic way to set a link on a button tag? <a href="#"><button>Click Me</button></a> <button><a href="#>Click Me</a></button> <button onclick="location.href='#'">Click Me</button>
@rick — <a class="looks-like-a-button" href="foo">Text Content</a> (The options you listed are, respectively, invalid, invalid and unnecessarily dependant on JavaScript).
2

As @Quentin’s answer explains, the type attribute is not required and it defaults to submit. 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 enclosing form element or a form element explicitly associated with the button 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, a button element defaults to type=button functionally. This implies that if a button element that has type defaulted changes its context (e.g., gets wrapped inside a form element), its functionality changes. Therefore, for clarity and safety, it is better to explicitly specify the type attribute, e.g. <button type=button> or <button type=submit>.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.