Form fields and labels
A <form> collects fields like <input> and <select> and sends them when you tap its submit button. Each field should have a <label> so people and screen readers know what to type.
Step by step
- Give every field an
idand aname; thenameis what gets sent. - Link a
<label>to its field by settingforto the field'sid. - Tapping a linked label usually puts the cursor in its field or ticks its checkbox.
- Pick the right
type:email,tel,number,checkboxorradio. - Give radio buttons of one group the same
name. - Remember that
placeholderis only a hint that disappears while typing, not a label. - A
<button>in a form sends it unless you set anothertype, anddisabledfields are left out when the form is sent.
The card, bug marked
1<form action="/contact" method="post">
2 <label for="name">Name</label>
3 <input type="text" id="name" name="name">
4 <label for="mail">E-mail</label>
5 <input type="email" id="email"
6 name="email">
7 <label for="phone">Phone</label>
8 <input type="tel" id="phone" name="phone"
9 placeholder="Optional">
10 <button type="submit">Send</button>
11</form>
This is how it's right
<form action="/contact" method="post">
<label for="email">E-mail</label>
<input type="email" id="email"
name="email">
<button type="submit">Send</button>
</form>
Exercises for this topic
- Find the bug.
- What does the menu show before anyone taps it?
- What happens when you tap the words "Send me the monthly newsletter"?
- Find the bug.
- You tap Search. Which address does the browser open?
- Find the bug.
- Find the bug.
- You tap Book without changing anything. Is "plan=trial" sent with the form?
- Find the bug.
- Find the bug.
- Can a visitor pick "Guitar" itself from this menu?
- Find the bug.
Key rule
Give every field a visible <label>, either with a for that matches the field's id exactly or by putting the field inside the label.
Where you'll meet this
You meet forms on contact pages, sign-ups, bookings and search boxes. Most form bugs are a for that does not match an id or a name with a typo.