nos
nos

Reputation: 20870

button types in html

I am new to html coding/javascript and the question is about button types. From w3schools website, there are three types of buttons

<button type="button|submit|reset">

I notice that if the button is set to be submit but there is no form around it, http POST is not sent. It makes sense because it does not know what http message to send. But why do we need an extra layer of form instead of defining the action and http method inside button?

This w3schools page gives examples to submit and reset buttons but not button buttons.

One code example I found on this w3schools page has no type

`<button onclick="myFunction()">Click me</button>`

Upvotes: 1

Views: 2147

Answers (3)

Dimava
Dimava

Reputation: 10899

  1. why do we need form for submit button?

We do not need form for submit button, we need submit button for form. The button submits form when clicked as well as enter in text field, but if you have only radios, ticks and textareas you have to make a button to be able to submit at all.

  1. when would one use the button-type button?

The type="button" is default for <button> tag. It goes from the <input> tag: if you write <input type="button">that will be the same as if you write <button>. So is for anothe <input> types: <input type="submit"> and <button type="submit"/> are same (note that button has closing tag unlike input, so you may put HTML in it.).buttons.

  1. does type default to some value? or is it not required to have a type?

see #2

Upvotes: 0

Mr Pixel
Mr Pixel

Reputation: 100

usually Forms are used to send

data(like user information for login) and

files to back server(like PHP)

Example:

<form method="POST" action="destination.php"> <input type="text" value="EMail" name="email"> <input type="submit" Value="OK"> </form>

When user clicks on submit button ~> form sends inputed email to destination.php Securely because of post method.

Upvotes: 1

BoeNoe
BoeNoe

Reputation: 562

Answer to the first question:

The HTML element <form> is used to define a form that used to collect information from the user. The form is what sends the information to another web page. So, the info can be processed.

For the second question:

It is good to use a simple button for a Javascript event. Such as the one you put in your question.

And finally the third question:

No, I don't think it has a default. But I would say if there was a default, I think it would be submit.

Upvotes: 2

Related Questions