Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's standard behavior when <button> element is clicked? Will it submit the form?

What is the standard behavior for when a <button> element is clicked in a form? Will it submit the form?

Question is about tag/element <button>, not <input type=button>.

like image 890
lovespring Avatar asked Jan 12 '11 10:01

lovespring


People also ask

What is the default behavior of submit button?

The default behavior of the button. Possible values are: submit : The button submits the form data to the server. This is the default if the attribute is not specified for buttons associated with a <form> , or if the attribute is an empty or invalid value.

What happens when submit button is clicked?

The form will be submitted to the server and the browser will redirect away to the current address of the browser and append as query string parameters the values of the input fields.

How do you display a form when a button is clicked?

To show or hide a form on a button click: Add a click event listener to the button element. Each time the button is clicked check if the form element is hidden. If the form is hidden, show it, otherwise hide the form.

What is use of button element in form?

Definition and Usage. The <button> tag defines a clickable button. Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.). That is not possible with a button created with the <input> element!


2 Answers

If the button is within a form, the default behavior is submit.

If the button is not within a form, it will do nothing.

BUT BE AWARE!

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".

Taken from http://www.w3schools.com/tags/tag_button.asp

like image 79
Jón Trausti Arason Avatar answered Oct 07 '22 20:10

Jón Trausti Arason


Yes it default to the submit type.

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.

See: http://www.w3.org/TR/html401/interact/forms.html#h-17.5

So when the button is inside a form it will submit it, when it's not inside a form, it still defaults to submit but does nothing (since there's no form associated with it).

As raRaRa has pointed out below older versions of IE have the button tag default type set to button: http://www.thefutureoftheweb.com/blog/button-wont-submit-in-ie

like image 34
Ivo Wetzel Avatar answered Oct 07 '22 19:10

Ivo Wetzel