The button
tag in HTML can be used to create a button within a form. This button can be used to submit the form, reset the form, or perform some other action. Here’s an example of how to use the button
tag:
<form action="/submit-form" method="post"> <fieldset> <legend>Personal Information:</legend> <label for="name">Name:</label> <input type="text" id="name" name="name"><br> <label for="email">Email:</label> <input type="email" id="email" name="email"><br> </fieldset> <button type="submit">Submit</button> <button type="reset">Reset</button> <button type="button" onclick="alert('Button clicked!')">Click me</button> </form>
In this example, we’re using the button
tag to create three buttons within the form. The first button has a type
attribute set to "submit"
, which will submit the form when the button is clicked. The second button has a type
attribute set to "reset"
, which will reset the form when the button is clicked. The third button has a type
attribute set to "button"
, which means it won’t perform any default action when clicked. Instead, we’ve added an onclick
attribute to display an alert when the button is clicked.
The fieldset
tag is used to group related form elements together. The legend
tag is used to provide a caption for the group of form elements. In this example, we’re grouping the name and email input fields together with a caption of “Personal Information”.
The input
tag has a type
attribute set to "text"
and "email"
, which creates a text input field and an email input field respectively. The id
attribute is used to uniquely identify each field, and the name
attribute specifies the name of the field, which will be used to retrieve the input value on the server.