HTML · Unit 3: Tables, forms and a profile card · Lesson 3 of 5
Input types
The type attribute changes what an input accepts.
Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.
The idea
The type attribute changes what an input accepts. type="email" checks for an @, type="number" brings up a number keypad on phones and type="checkbox" makes a box to tick.
required stops a form from being sent while that box is empty. min and max limit a number.
The right type helps everyone: phones show a fitting keyboard, and browsers can check answers before anything is sent.
Worked example
HTML
<label for="age">Age</label>
<input id="age" name="age" type="number" min="1" max="120">
<input id="news" name="news" type="checkbox">
<label for="news">Send me the newsletter</label>
Key terms
- Input
- A form control. Its type, like text, email, number or checkbox, decides what it accepts.
Practise it in Code Academy
It has 4 exercises (multiple choice, fill in the blank, fix the broken code and write code from a prompt) and takes about 6 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.
Every lesson is free: no ads, no lives and no paywall.