HTML HTML Tutorial HTML Forms HTML Graphics HTML Media HTML APIs HTML Tags



HTML Tag: button

The HTML button tag is used to create a clickable button on a web page. It is one of the most commonly used HTML tags and is used to create buttons for various purposes such as submitting a form, triggering an event, or navigating to a different page.

Brief Explanation about HTML Tag: button

The button tag is a versatile HTML tag that can be used in a variety of ways. It can be used to create a button that submits a form, a button that triggers an event, or a button that navigates to a different page. The button tag can also be used in conjunction with JavaScript to create more complex functionality.

The button tag has several attributes that can be used to customize its appearance and behavior. The most commonly used attributes are:

  • type: Specifies the type of button. The possible values are "submit", "reset", and "button".
  • name: Specifies the name of the button.
  • value: Specifies the value of the button.
  • disabled: Specifies whether the button is disabled or not.
  • onclick: Specifies the JavaScript code to be executed when the button is clicked.

Code Examples in Per Tags

Here are some examples of how the button tag can be used:

Example 1: Submit Button

This example shows how to create a button that submits a form:

  <form action="submit.php" method="post">
    <input type="text" name="name">
    <button type="submit">Submit</button>
  </form>

In this example, the button tag has a type attribute of "submit", which tells the browser to submit the form when the button is clicked.

Example 2: Event Trigger Button

This example shows how to create a button that triggers an event:

  <button onclick="alert('Hello, World!')">Click Me</button>

In this example, the button tag has an onclick attribute that specifies the JavaScript code to be executed when the button is clicked. In this case, the code displays an alert box with the message "Hello, World!".

Example 3: Navigation Button

This example shows how to create a button that navigates to a different page:

  <button onclick="window.location.href='http://www.example.com'">Go to Example.com</button>

In this example, the button tag has an onclick attribute that specifies the JavaScript code to be executed when the button is clicked. In this case, the code sets the location of the current window to "http://www.example.com", which causes the browser to navigate to that page.

References

Activity