There are three ways you can use button by setting the three different type attribute.
When you set the type
attribute of a button to submit
,
pressing the button will submit the form that contains the button.
This is the default behavior when you have not applied the type
attribute.
Submit button have some additional attributes listed as follows.
The following code shows how you can apply these attributes to the button element.
<!DOCTYPE HTML> <html> <body> <form> <p> <label for="fave">Fruit: <input autofocus id="fave" name="fave" /></label> </p> <p> <label for="name">Name: <input id="name" name="name" /></label> </p> <button type="submit" formaction="http://example.com/form" formmethod="post">Submit Vote</button> </form> </body> </html>
The code above omitted the action and method attributes from the form element and
provided the configuration through the formaction
and formmethod
attributes on the button element.
If you set the type
attribute from a button
to reset
,
pressing the button causes all of the input elements in the
form to be reset to their initial state.
There are no additional attributes for reset button.
The following code shows the addition of a reset button to the HTML document.
<!DOCTYPE HTML>
<html>
<body>
<form method="post" action="http://example.com/form">
<p>
<label for="fave">Fruit:
<input autofocus id="fave" name="fave" /></label>
</p><!--from w ww . j av a2s .com-->
<p>
<label for="name">Name: <input id="name" name="name" /></label>
</p>
<button type="submit">Submit Vote</button>
<button type="reset">Reset</button>
</form>
</body>
</html>
If you set the type
attribute to button
, you create a normal button.
It has no special meaning and won't do anything when you press it.
The following code shows a generic button.
<!DOCTYPE HTML>
<html>
<body>
<form method="post" action="http://example.com/form">
<p>
<label for="fave">Fruit: <input autofocus id="fave"
name="fave" /></label>
</p><!--from ww w. j a va 2 s. co m-->
<p>
<label for="name">Name: <input id="name" name="name" /></label>
</p>
<button type="button">
Do <strong>NOT</strong> press this button
</button>
</form>
</body>
</html>
You can style the text contained in the button element.
<html>
<body>
<form action="" method="post">
<button type="submit">Submit</button>
<br /><br />
<button type="reset"><b>Clear this form</b> I want to start again</button>
<br /><br />
<button type="button"><img src="http://www.java2s.com/style/download.png" alt="submit" /></button>
</form>
</body><!-- w w w.jav a 2 s. c o m-->
</html>