Lesson 38 of 51 · Module 6, lesson 4 of 6 · 3 min

Buttons

Use the three button types correctly, and know when to use a button and when a link.

What you'll learn

  • The three button types: submit, reset and button
  • Why you should always write the type
  • Why a button must be a <button>, not a styled link or box

Why this matters

Buttons look simple, but a wrong type is one of the most common form bugs: a button that should open a menu suddenly sends the whole form. And buttons made from other elements often cannot be reached with a keyboard at all.

Three types

typeWhat it does
submitSends the form. This is the default inside a form.
resetClears every field back to its starting value.
buttonDoes nothing on its own. JavaScript gives it a job later.
html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Search recipes</title>
</head>
<body>
  <h1>Search recipes</h1>
  <form>
    <p>
      <label for="q">Search</label>
      <input id="q" name="q" type="search">
    </p>
    <p>
      <button type="submit">Search</button>
      <button type="reset">Clear</button>
      <button type="button">Show tips</button>
    </p>
  </form>
</body>
</html>

What you'll see: a search box and three buttons. Search sends the form. Clear empties the box. Show tips does nothing yet — it waits for JavaScript.

The "Search recipes" page with a search box and three buttons in a row: Search, Clear and Show tips.

Always write the type

Inside a form, a <button> with no type is a submit button. So a button that you meant for something else — "Show tips" — will send the form. Writing type every time avoids this surprise.

Button text

  • Say what happens: Sign up, Send feedback, Search — not Submit or OK.
  • Use reset rarely. People press it by accident and lose everything they typed.

A button must be a button

You may see "buttons" made from a <div> or a link. Avoid that:

  • <button> can be reached with Tab and pressed with Enter or Space. A <div> cannot, without extra work.
  • Screen readers announce "button". A <div> is announced as plain text.

Rule of thumb: if it goes somewhere, use a link <a href>. If it does something on this page, use a <button>.

You may also see <input type="submit" value="Send">. It works the same as <button type="submit">Send</button>, but <button> can contain other elements, such as an icon, so it is the usual choice today.

Try it yourself

  1. Select the example and choose Try it.
  2. Type something, then press Clear.
  3. Remove type="button" from Show tips and press it. The form is sent — the default at work. Put the type back.

Common mistakes

  • A button with no type inside a form. It sends the form. Fix: always write the type.
  • A <div> or <span> as a button. Keyboard and screen reader users cannot use it. Fix: <button>.
  • A link used as a button (<a href="#">). Fix: a button for actions, a link for places.
  • "Submit" as the text. Fix: say what the button does.

Check yourself

  1. What does a <button> inside a form do if it has no type?
  2. Which type does nothing until JavaScript gives it a job?
  3. Something takes the visitor to another page. Link or button?

Answers

  1. It submits the form.
  2. type="button".
  3. A link.

Next: the browser can check the answers before a form is sent — you only have to tell it the rules.

Chat with us on WhatsApp