user4855927
user4855927

Reputation:

What's the difference between <button> and <button type="button">

I've run into this html:

<button type="button></button>

What's the difference between the html above and this:

<button></button>

Upvotes: 2

Views: 104

Answers (2)

jfriend00
jfriend00

Reputation: 707326

From the MDN page on the <button> tag:

For the type attribute of the <button> tag, the possible values are:

submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.

reset: The button resets all the controls to their initial values.

button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.

So, if the button is inside a <form> and there is no type specified, it defaults to a submit button. So, if you want it to be just a normal button that does not default to a submit button, then you specify type="button".

Upvotes: 1

imGaurav
imGaurav

Reputation: 1053

1.only button has type = "submit" on it by default which on click submits form . 2.button type="button" is only clickable and does not have any event handler on it.You can assign one if you want

Upvotes: 1

Related Questions