button Tag in HTML
The <button> tag in HTML is used to create a clickable button on a web page. The button can be used to perform an action when it is clicked, such as submitting a form, opening a new page, or triggering a JavaScript function.
Syntax:
The basic syntax for the <button> tag is as follows:
<button>Button Text</button>
Attributes:
The <button> tag can have several attributes that can be used to customize its appearance and behavior. Here are some of the most commonly used attributes:
- type: This attribute specifies the type of button. It can be set to “submit” to submit a form, “reset” to reset a form, or “button” to create a regular button that doesn’t perform any action by default.
<button type=”submit”>Submit</button>
name: This attribute specifies the name of the button. It is useful when submitting a form, as it allows the server to identify which button was clicked.
<button type=”submit” name=”submit-btn”>Submit</button>
value: This attribute specifies the value of the button. It is also useful when submitting a form, as it allows the server to determine which value was selected.
<button type=”submit” value=”1″>Yes</button>
<button type=”submit” value=”0″>No</button>
disabled: This attribute disables the button and prevents it from being clicked.
<button type=”submit” disabled>Submit</button>
onclick: This attribute specifies a JavaScript function to be executed when the button is clicked.
<button onclick=”myFunction()”>Click me</button>
Example:
Here is an example of a <button> tag with some of the attributes mentioned above:
<button type=”submit” name=”submit-btn” value=”1″ onclick=”myFunction()”>Submit</button>
In this example, the button will submit a form with the name “submit-btn” and value “1” when clicked, and will also execute the “myFunction()” JavaScript function.