Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  66] [ 2]  / answers: 1 / hits: 53511  / 7 Years ago, fri, january 27, 2017, 12:00:00

Is <button type=button> any different from a simple <button> with a blank or missing type attribute? MDN and the HTML5 spec say that type=button is for buttons whose purpose is to trigger custom JavaScript, but isn't that also what a <button> does by default?


More From » html

 Answers
15

Yep, there's a reason - but (usually) only if you're in a <form> element.


If you include a button in a form element without specifying it's just a regular button, it defaults to a submit button.


<form>
<button>I will submit the form when clicked!</button>
</form>

vs


<form>
<button type='button'>I won't!</button>
</form>

The first one is assumed to be type=submit since a type attribute hasn't been specified.




If you are not in a <form> element, the button won't have anything to submit, so it doesn't matter as much. :)


Semantics usually become important at some point in your application's lifetime, though, so it's a good idea to make a habit of specifying the type.




The only other reason that could be relevant is if there's a styling rule that specifies [type=button] or something. That's not recommended, though.


[#59177] Wednesday, January 25, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kalynnkathrynd

Total Points: 273
Total Questions: 101
Total Answers: 93

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
;