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,resetandbutton- 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
type | What it does |
|---|---|
submit | Sends the form. This is the default inside a form. |
reset | Clears every field back to its starting value. |
button | Does nothing on its own. JavaScript gives it a job later. |
<!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.
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 withTaband pressed withEnterorSpace. 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
- Select the example and choose Try it.
- Type something, then press Clear.
- 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
typeinside 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
- What does a
<button>inside a form do if it has notype? - Which type does nothing until JavaScript gives it a job?
- Something takes the visitor to another page. Link or button?
Answers
- It submits the form.
type="button".- A link.
Next: the browser can check the answers before a form is sent — you only have to tell it the rules.

