Sub topic 7
Checkboxes, Radio Buttons, and Select Menus
This chapter assumes you know how a form element submits name and value pairs, how label elements attach to controls, and how name and value attributes become submitted data. Here we cover the three controls for picking from a set of options: checkboxes, radio buttons, and select menus. We look at how each one submits data and where each one commonly goes wrong.
Choosing a control
The right control depends on how many options a user may pick and how many options exist.
| Situation | Control |
|---|---|
| One independent yes/no choice | Single checkbox |
| Zero or more choices from a short list | Group of checkboxes |
| Exactly one choice from a short list (about 2 to 5) | Radio group |
| One choice from a long list | `select` |
| Several choices from a long list | `select multiple` (use with care) |
Checkboxes
A checkbox is an input with type="checkbox". It has two states, checked and unchecked, and a user toggles it independently of other controls.
<form action="/signup" method="post">
<label>
<input type="checkbox" name="newsletter" value="yes"> Send me the newsletter
</label>
<button type="submit">Create account</button>
</form>What a checkbox submits
Only a checked checkbox is a successful control, which means the browser includes it in the submission. If the box is checked, the server receives newsletter=yes. If it is unchecked, the server receives nothing for that name. The field is absent, not sent as false or as an empty string.
If you omit the value attribute, the submitted value is the string on. This surprises many people, because newsletter=on looks like a boolean but is only a default string.
Server code therefore tests for presence of the key, not for a particular value. A handler that reads newsletter and treats a missing key as "off" is correct. A handler that expects newsletter=false for unchecked boxes will never see it.
Checkbox groups
Several checkboxes can share one name. Each checked box adds its own pair, so the name repeats in the submission.
<form action="/survey" method="post">
<fieldset>
<legend>Which languages do you use?</legend>
<label><input type="checkbox" name="lang" value="js"> JavaScript</label>
<label><input type="checkbox" name="lang" value="py"> Python</label>
<label><input type="checkbox" name="lang" value="go"> Go</label>
</fieldset>
<button type="submit">Send</button>
</form>If a user checks JavaScript and Go, the body is lang=js&lang=go. Most server frameworks need to be told to read repeated keys as a list. In JavaScript, FormData keeps both entries, but Object.fromEntries keeps only the last one. Use data.getAll('lang') to get every value.
The checked attribute and the checked property
The checked attribute sets the default state when the page loads. The checked property on the element reflects the current state. Resetting the form returns each box to its attribute-defined default.
const box = document.querySelector('input[name="newsletter"]');
console.log(box.checked); // current state, true or false
box.checked = true; // changes state, not the attributeThe indeterminate state
A checkbox can also be visually "indeterminate", a dash instead of a tick. You can only set this with JavaScript through box.indeterminate = true. It is a display state only. It does not change whether the box is checked and has no effect on what is submitted. It is typically used for a "select all" box when only some child boxes are checked.
Radio buttons
A radio button is an input with type="radio". Radio buttons that share a name (within the same form) form a group, and the browser allows at most one to be selected at a time.
<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>
<label><input type="radio" name="plan" value="team"> Team</label>
</fieldset>
<button type="submit">Continue</button>
</form>What a radio group submits
The group submits one pair, using the name and the value of the selected button, for example plan=pro. If no button is selected, nothing is submitted for that name.
Radio group behavior and pitfalls
- Grouping is by name. Buttons with different names are separate groups, and the user can select one in each. A typo in one
namesilently breaks the group. - A selected radio cannot be deselected by clicking it again. Once a user has chosen, the only ways back to "nothing chosen" are a form reset or script. If "no answer" is a valid choice, add an explicit option for it, such as a "None" button.
- Set a default deliberately. If you mark one button
checked, the field always has a value, and users may submit it without reading. If the choice matters, leave all buttons unchecked and addrequiredso the user must decide. - Every button needs its own
value. Without it, each submitson, and the server cannot tell them apart. - Keyboard use differs from checkboxes. Tab moves focus into and out of the group as a whole. Arrow keys move the selection among the buttons. Space selects the focused one. This is built in, as long as the buttons share a name.
Required radio groups
required on any radio in a group makes the whole group required. Put it on every button anyway, so the markup reads clearly.
<fieldset>
<legend>Shipping speed</legend>
<label><input type="radio" name="speed" value="standard" required> Standard</label>
<label><input type="radio" name="speed" value="express" required> Express</label>
</fieldset>Select menus
A select element shows a list of option elements as a drop-down. It submits the value of the chosen option under the select's name.
<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>Value versus label
The text between the option tags is what the user sees. The value attribute is what is submitted. If an option has no value, the browser submits its text content instead. Always set value explicitly so that changing a visible label, or translating it, does not change the data your server receives.
Placeholder options and required
A select without multiple always has a selected option. If none carries selected, the first one is chosen. That means a form can be submitted with an option the user never looked at.
To force a decision, make the first option a placeholder with an empty value, and add required to the select. Validation treats an empty-valued placeholder in the first position as "no choice", and blocks submission. This works only when the placeholder is the first option and its value is the empty string.
Grouping options
An optgroup with a label attribute groups related options under a non-selectable heading.
<label for="city">City</label>
<select id="city" name="city">
<optgroup label="Germany">
<option value="ber">Berlin</option>
<option value="muc">Munich</option>
</optgroup>
<optgroup label="United States">
<option value="nyc">New York</option>
<option value="sfo">San Francisco</option>
</optgroup>
</select>The group label is never submitted.
Multiple selection
Adding multiple turns the drop-down into a list box where users select several options, usually with Ctrl or Cmd plus click. Each selected option adds a pair with the same name, as with a checkbox group.
<label for="topics">Topics</label>
<select id="topics" name="topics" multiple size="4">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="js">JavaScript</option>
<option value="a11y">Accessibility</option>
</select>Many users do not discover the modifier-key gesture, and touch and keyboard use is clumsy. For a short list, a checkbox group is easier to use.
Reading a select in script
const select = document.querySelector('#topics');
const chosen = Array.from(select.selectedOptions, (o) => o.value);
console.log(chosen);For a single select, select.value gives the chosen value.
Disabled controls
A disabled control is never submitted, and users cannot interact with it. A disabled option inside a select cannot be chosen. If you need to show a value that must still be sent, use a readonly text input or a hidden input. Checkboxes and radio buttons do not support readonly, so only disabled removes interaction.
Quick comparison of submitted data
Common mistakes
- Expecting an unchecked checkbox to send
false. It sends nothing. - Giving radio buttons different
namevalues, so they do not behave as a group. - Omitting
valueon options, so labels become the submitted data. - Leaving a default option selected in a
selectand assuming the user chose it. - Using
Object.fromEntries(new FormData(form))with a checkbox group, which drops all but the last value. - Using
disabledon a control whose value the server needs.
Exercise
A form has three checkboxes that all use name="lang" with values js, py, and go. The user checks js and go, then the page runs Object.fromEntries(new FormData(form)). What does the result contain, and how do you get both values?
Show answer
The result is { lang: "go" }, because Object.fromEntries keeps only the last value for a repeated name. Call new FormData(form).getAll("lang") to get ["js", "go"].
Exercise
A select element named plan has a first option written as <option>Choose a plan</option> with no value attribute, and the select has required. Why does the form still submit without a real choice, and how do you fix it?
Show answer
With no value attribute, the option's value is its text, "Choose a plan", which is not empty, so required is satisfied. Change it to <option value="">Choose a plan</option> so validation treats it as no choice.