Forms: Select & Radio — HTML

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…

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:

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>

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>

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>

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.

What you will learn