Skip to main content

HTML Tags

HTML <button> Tag

Written by Updated HTML <button> Tag exercises

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>
AttributeWhat it does
typesubmit sends the form (the default inside a form), reset clears it, button does nothing on its own, for your JavaScript.
nameThe name sent with the form when this button is used to submit it.
valueThe value sent under that name.
disabledThe button cannot be clicked and is greyed out.
formThe 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+
YesYesYesYesYesYes

Good to know

  • Use <button> for actions on the page and <a> for going to another page. Do not swap them.
  • A button that contains only an icon needs a name for screen readers, such as aria-label="Close".
  • Avoid reset buttons in most forms; people click them by mistake and lose everything they typed.

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.