Sub topic 7
Checkboxes, radio buttons, and select menus
This chapter assumes you know how a form submits name=value pairs, how labels attach to controls, and basic HTML nesting. Here you will pick the right choice control and predict exactly what it sends.
Three controls, three questions
Choice controls answer different questions about the data.
| Control | Question it answers | Submits |
|---|---|---|
| Checkbox | Is this option on or off? Or which of several apply? | One pair per checked box |
| Radio group | Which one of a small set? | One pair for the selected radio |
| Select menu | Which one (or several) from a longer list? | The `value` of each chosen option |
Radio buttons
Radio buttons that share the same name form one group. The browser lets the user select only one member of a group. Each radio needs its own value, because that value is what gets sent.
Put checked on the default option. Without a default, the group starts empty. Once a user picks a radio, they cannot unselect it, so a group with no default can submit nothing at all if left alone. Add required to one radio in the group to force a choice.
Checkboxes
A checkbox is a standalone on/off switch. A checked box sends its name and value. An unchecked box sends nothing, so the server never sees the field.
If you omit value, a checked box sends the string on. Give it a meaningful value.
To collect several answers, reuse one name across many checkboxes. The form then sends the name once per checked box.
Select menus
A select holds option elements. It submits the value of the chosen option. If an option has no value attribute, its text content is sent instead.
- Add
multipleto allow more than one choice. Each selected option is sent as a separate pair. - Add
selectedto an option to preselect it. - Use
optgroupwith alabelto group long lists.
A single select with no selected option submits its first option. To force a deliberate choice, make the first option a placeholder with an empty value and add required to the select.
Putting it together
<form action="/signup" method="post">
<fieldset>
<legend>Plan</legend>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
</fieldset>
<label for="country">Country</label>
<select id="country" name="country" required>
<option value="">Choose a country</option>
<option value="de">Germany</option>
<option value="us">United States</option>
</select>
<label>
<input type="checkbox" name="newsletter" value="yes"> Send me the newsletter
</label>
<button type="submit">Create account</button>
</form>If the user keeps Free, picks Germany, and ticks the newsletter box, the body is plan=free&country=de&newsletter=yes. If they leave the box unticked, newsletter is absent.
Common errors
- Different names on radios. Each radio then forms its own group, and the user can select all of them.
- Missing
valueon a checkbox. The server receiveson, which says nothing about what was chosen. - Expecting
falsefor an unchecked box. Nothing is sent. Treat a missing field as false on the server. - Reading repeated names with
Object.fromEntries. It keeps only the last value. UseFormData.getAll('name')for multiple checkboxes or amultipleselect. - Labeling only the group. A
legendnames the group, but each radio and checkbox still needs its own label.
Exercise
A form has three checkboxes that all use name="topic" with values "html", "css", and "js". The user ticks the first and third. What does the form send, and how would you read both values in JavaScript from a FormData object?
Show answer
It sends topic=html&topic=js. In JavaScript, call data.getAll('topic'), which returns ["html", "js"]. Object.fromEntries(data) would keep only "js".
Exercise
A required select starts with the option <option value="">Choose a country</option>. Why does this stop users from submitting without choosing?
Show answer
The placeholder is the selected option, and its value is an empty string. A required select treats an empty value as missing, so the browser blocks submission until the user picks another option.