The <button>
element in HTML is used to create clickable buttons that can trigger actions, like submitting a form, performing a JavaScript function, or navigating to another page.
The <button>
element is commonly used when you need to trigger an action, such as submitting a form, interacting with a user interface, or triggering JavaScript functions. It's often used within forms or with JavaScript to create interactive elements.
The <button>
element has different types that control its behavior within a form:
type="button"
- A general-purpose button that does not submit the form. Used for triggering JavaScript functions or other actions.type="submit"
- A button that submits the form. The form data is sent to the server for processing.type="reset"
- A button that resets all the form fields to their default values.<button type="type">Button Text</button>
<!DOCTYPE html> <html> <head> <title>Button Example</title> </head> <body> <form> <button type="submit">Submit</button> <button type="button" onclick="alert('Button clicked!')">Click Me</button> <button type="reset">Reset</button> </form> </body> </html>
type
attribute for buttons within forms to avoid unexpected behavior (e.g., form submission or reset).type="button"
for buttons that do not submit forms but perform other actions, like triggering JavaScript.The <button>
element is a versatile tool for creating interactive elements in HTML. By using the right type
and ensuring good accessibility practices, you can improve user experience and functionality on your webpage.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!