In this lesson, you gave the work-day sign-up form’s email, phone and hours fields the types that fit their data, and told the browser which saved detail belongs in each field it can fill.
Key Points
- The
typeattribute says what kind of data an<input>holds, and the browser picks its tools to match. Like a<button>, an input gets a default when you leave itstypeout: for an input, that’stype="text", a plain box, which is why every field behaved the same. - Where the comparison stops: a type works a little like a variable’s type in a typed language, but a wrong value never throws an error. Some types filter what goes in while someone fills in the field, and the browser can check the value before sending. That check comes later in the course.
type="email"looks no different on a laptop. On a phone, the keyboard has an at key on its first page instead of hiding it behind another page of keys.type="tel"brings up a dial pad on a phone. People write phone numbers in many ways, soteldoesn’t check the format at all. Typed on a laptop, it even accepts the words “call Ana”. It only changes the keyboard.type="number"brings up a number keypad on a phone. On a laptop, two small arrows appear inside the field when you point at it, and the up arrow key steps the empty field to 1.- Count it or dial it? A number field lets in only what a number can hold: typing
abcleaves it empty, and typing0142 555gives0142555, without the space. Phone numbers come with spaces, dashes and plus signs, so usenumberfor things you count andtelfor phone numbers. - The
autocompleteattribute tells the browser which saved detail goes in a field. Without it, Chrome guesses from the field’s name and label. Its value is a token from a fixed list in the HTML standard, so you can’t make one up. The phone field’snameisphone, but its token istel, because that’s the word on the list. This form needs the tokensname,emailandtel. The hours field gets none, because no saved detail fits it. - Why a token beats a guess: Chrome might have guessed these three right anyway, but a guess works from the names and labels you picked, and those could be anything. A token means the same to every browser. A visitor picks their saved entry once, and name, email and phone fill in together.
- More types exist, such as
urlanddate. MDN’s list of input types has them all, and MDN’s autocomplete page lists every token.
Final Code
The first four fields of the sign-up form in events/work-day.html now read:
<p>
<label for="name">Your name</label>
<input id="name" name="name" autocomplete="name">
</p>
<p>
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email">
</p>
<p>
<label for="phone">Phone (for example, 555 0142)</label>
<input id="phone" name="phone" type="tel" autocomplete="tel" placeholder="555 0142">
</p>
<p>
<label for="hours">Hours you can stay</label>
<input id="hours" name="hours" type="number">
</p>
The task and the time slot are still text boxes. In the next lesson, Group Choices in a Form, you’ll turn them into choices a volunteer can pick.