- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML <button> Tag
HTML Tags
HTML <button> Tag
The button tag makes a clickable button. Inside a form, a plain button submits the form by default, which surprises many people: a "Show password" button that suddenly sends the form. Always set the type.
<button> works with the keyboard and screen readers automatically, which a clickable <div> does not. It can contain text, icons and images, unlike <input type="button">.
Syntax
html
<button type="submit" name="btnName" value="buttonValue">Click Me</button>| Attribute | What it does |
|---|---|
| type | submit sends the form (the default inside a form), reset clears it, button does nothing on its own, for your JavaScript. |
| name | The name sent with the form when this button is used to submit it. |
| value | The value sent under that name. |
| disabled | The button cannot be clicked and is greyed out. |
| form | The id of the form it belongs to, if it sits outside. |
Example
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button Example</title>
</head>
<body>
<h2>Button Examples</h2>
<form action="/samples/form_processor.php" method="post">
<button type="submit" name="submitBtn" value="Submit">Submit Form</button>
<button type="reset">Reset Form</button>
<button type="button" onclick="alert('Hello!')">Click Me</button>
</form>
</body>
</html>Three buttons appear. "Submit Form" sends the form, "Reset Form" clears its fields, and "Click Me" shows an alert. Only the last one does not touch the form, because its type is button.
Always set the type
The bug and the fix
html
<form>
<input type="password" id="pw">
<button onclick="togglePassword()">Show</button> <!-- submits the form! -->
<button type="button" onclick="togglePassword()">Show</button> <!-- correct -->
</form>Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Good to know
HTML <button> Tag exercises
Add a submit button
In a form add a button that sends the form, with the text Send. Write its type.
Result
Check your answer to see it here.
