In the previous lesson, visitors typed their own answers. But for a delivery city or a delivery option, typing takes effort, and the answers come in many forms: "London, UK", "london", "LONDON". It is easier when visitors only pick from choices you have prepared. HTML has three tools for that:
<select>: a dropdown. Visitors pick one from a list that opens when clicked.- Radio buttons: small circles in front of each choice. Visitors pick one from a few choices that are all visible.
- Checkboxes: tick boxes. Visitors can pick more than one.
Dropdowns with <select>
Here is a dropdown for choosing a delivery city:
<form>
<label for="city">Delivery city</label>
<select id="city" name="city">
<option value="">Choose a city</option>
<option value="london">London</option>
<option value="manchester">Manchester</option>
<option value="leeds">Leeds</option>
<option value="bristol">Bristol</option>
</select>
</form>
<select>is the dropdown box. Itsidconnects it to<label for="city">, just like on an<input>.nameis the name given to this choice when the form is sent.- Each
<option>is one choice. Write it inside the<select>, each with its own</option>. London, the text between<option>and</option>, is what visitors see.value="london"is the value sent to the server.- The first choice, "Choose a city", is only a hint for visitors. That is why its
valueis left empty:value="".
The result is a box that says "Choose a city" with a small arrow. When a visitor picks Manchester, the form sends city=manchester. city comes from the name, and manchester from the value. The visible text is not sent. (If an <option> has no value, its text is sent.)
In a dropdown, visitors can pick only one choice, unless you add the multiple attribute to <select>: <select multiple>.
Radio buttons: pick one of several
Here are three radio buttons for choosing a delivery option:
<p>Delivery</p>
<label><input type="radio" name="delivery" value="regular"> Regular</label>
<label><input type="radio" name="delivery" value="express"> Express</label>
<label><input type="radio" name="delivery" value="pickup"> Pick up in store</label>
type="radio"makes the<input>show as a round button.name="delivery"is exactly the same on all three. That sharednameis what makes them one group: when you pick one, the others are no longer picked.- The
valueis different on each radio, because thevalueis what is sent. Pick Express and the form sendsdelivery=express. - The
<label>wraps the input and its words. This connects the label to the input withoutforandid: click the word "Express" and its radio is picked.
What if the names are different? Each radio becomes a group of its own, so they can all be picked at once.
Checkboxes: pick as many as you like
Here are two checkboxes for choosing hobbies:
<p>Your hobbies</p>
<label><input type="checkbox" name="hobby" value="coding"> Coding</label>
<label><input type="checkbox" name="hobby" value="sports"> Sports</label>
type="checkbox"makes the<input>show as a tick box. Each box works on its own, so more than one can be ticked.name="hobby"is the same on every box, so they are sent as one group of answers. Tick both and the form sendshobby=codingandhobby=sports.- The
valueis different on each box. Capital letters count:value="Coding"andvalue="coding"are two different values.
So radio buttons allow one choice, and checkboxes allow many. Both need the same name for one group, and a different value for each choice. Write the values inside quote marks: name="hobby", not name=hobby.
⚠️ Choices that aren't sent, or aren't in the dropdown
The words next to a radio are for visitors to read. They are not what is sent. The value is sent, so give every radio and checkbox a value of its own.
An <option> written outside the <select> is not part of the dropdown. Write every <option> before </select>.
TL;DR: A dropdown (a list of choices that opens when clicked) is a <select> holding <option>s. Add multiple if visitors may pick more than one. Radio buttons (type="radio") allow only one choice, as long as every radio in the group has the same name. Checkboxes (type="checkbox") allow many. What is sent is the name and value pair, not the visible text, so each choice needs its own value.
⚡ A dropdown
Write:
<label for="city">Delivery city</label>
<select id="city" name="city">
<option value="">Choose a city</option>
<option value="london">London</option>
<option value="manchester">Manchester</option>
</select>
Guess first: when a visitor picks Manchester, what is sent?
city=manchester. city comes from the name, and manchester from the value. The visible text is not sent.
Meaning: <select> makes a dropdown: a list of choices that opens when clicked. Each <option> is one choice, written inside the <select>. Visitors can pick only one. Want to allow more than one? Add the multiple attribute: <select multiple>.
⚡ Radio buttons: pick one
Write:
<label><input type="radio" name="delivery" value="regular"> Regular</label>
<label><input type="radio" name="delivery" value="express"> Express</label>
Guess first: what happens if the names are different?
All the radios can be picked at once, because each radio becomes a group of its own.
Meaning: type="radio" makes a round button for one choice. The same name makes the radios one group: when you pick one, the others are no longer picked. Their values are different, because the value is what is sent.
⚡ Checkboxes: pick many
Write:
<label><input type="checkbox" name="hobby" value="coding"> Coding</label>
<label><input type="checkbox" name="hobby" value="sports"> Sports</label>
Meaning: type="checkbox" makes a tick box. Each box works on its own, so more than one can be ticked. Tick both and the form sends hobby=coding and hobby=sports.