Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to specify type="submit" on submit buttons?

Previously, when I've had a form with a single submit button on it, I've usually not put the type="submit" attribute in there. I've never observed this causing problems, and had believed that "submit" was the default type for a button and that I could rely on this behaviour.

However, w3schools and MDN disagree on whether I am right.

w3schools claims:

Tip: Always specify the type attribute for the <button> element. Different browsers may use different default types for the <button> element.

whereas MDN claims:

  • submit: ... This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.

Who is right and who is wrong - both according to spec, and in real browsers?

like image 210
Mark Amery Avatar asked Dec 16 '13 12:12

Mark Amery


People also ask

Can a button be of type submit?

The <button> tag with a type="submit" attribute creates a submit button.

Should I use input type submit or button?

input suggests that the control is editable, or can be edited by the user; button is far more explicit in terms of the purpose it serves. Easier to style in CSS; as mentioned above, FIrefox and IE have quirks in which input[type="submit"] do not display correctly in some cases.

Is input type submit is the same as button type submit?

Both <button type="submit"> and <input type="submit"> display as buttons and cause the form data to be submitted to the server. The difference is that <button> can have content, whereas <input> cannot (it is a null element).

Is button type submit by default?

Definition and Usage The type property sets or returns the type of a button. Tip: Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and in the W3C specification) it is "submit".


1 Answers

MDN has some spec links near the bottom of its article which confirm that what it says is correct: the default type of a button element is submit.

W3C HTML 4.01:

type = submit|button|reset [CI]
This attribute declares the type of the button. Possible values:

  • submit: Creates a submit button. This is the default value.
  • reset: Creates a reset button.
  • button: Creates a push button.

W3C HTML5:

The type attribute controls the behavior of the button when it is activated. It is an enumerated attribute. The following table lists the keywords and states for the attribute — the keywords in the left column map to the states in the cell in the second column on the same row as the keyword.

Keyword | State         | Brief description
--------+---------------+------------------
submit  | Submit Button | Submits the form.
reset   | Reset Button  | Resets the form.
button  | Button        | Does nothing. 

The missing value default is the Submit Button state.

What W3Schools is saying is that you should always specify the attribute to ensure as consistent behavior across browsers as possible. That doesn't imply that the attribute cannot legally be omitted.

like image 99
BoltClock Avatar answered Sep 20 '22 13:09

BoltClock