Three controls let a visitor choose, and they differ in a way that matters. They differ in how many answers are allowed, and in how obvious the available options are.
Getting that wrong produces forms that people fill in incorrectly, not forms that fail outright. The mistakes are quiet, so you need a clear rule to fall back on.
The previous lesson in this module covered "Why Do Labels Matter for Every Form Field?", and the next lesson in this module is about "What Happens When a Form Is Submitted?".
What is the actual difference?
A radio button lets you choose exactly one option from a small set.
A checkbox lets you choose any number of options, including none at all.
A select is a dropdown. It also allows one choice, but it hides the options until the visitor opens it.
<!-- radio: one answer, all options visible -->
<label><input type="radio" name="plan" value="free"> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
<!-- checkbox: any number of answers -->
<label><input type="checkbox" name="topics" value="java"> Java</label>
<label><input type="checkbox" name="topics" value="css"> CSS</label>
<!-- select: one answer, options hidden until opened -->
<select name="plan">
<option value="free">Free</option>
<option value="pro">Pro</option>
</select>The radio group is defined by a shared name. Two radios with the same name form one group, and the browser lets only one of them be checked.
Checkboxes with the same name are independent. Each one is checked or not on its own, and all of them are submitted together.
When should you use the radio buttons?
Use them when there are two to about seven options, and the visitor should compare them.
Because every option is visible, radio buttons let somebody see the whole choice at once. That is exactly what you want for a pricing plan or a delivery speed.
<fieldset>
<legend>Delivery speed</legend>
<label for="speed-standard">
<input type="radio" id="speed-standard" name="speed" value="standard" checked>
Standard (5 days)
</label>
<label for="speed-express">
<input type="radio" id="speed-express" name="speed" value="express">
Express (2 days)
</label>
</fieldset>Two details matter here.
The checked attribute makes one option the default. That is a convenience for the visitor, and it makes the group valid without any action.
The fieldset with the legend gives the group a name that is announced before each option, so the visitor always knows what question they are answering.
When should you use the checkboxes?
Use them when several answers are valid, or when the answer is genuinely a yes or a no.
The single checkbox for the terms and conditions is a checkbox and not a radio. The answer is binary, but the "tick to agree" meaning is well established.
<label for="terms">
<input type="checkbox" id="terms" name="terms" value="accepted">
I have read and accept the terms
</label>For a group of checkboxes, the submitted data contains one entry per checked box, all with the same name and different values.
That is worth knowing when you write the server side, because the value of topics may be a single string or a list, and it depends on how many boxes were ticked.
nothing ticked -> (the field is absent from the request)
one ticked -> topics=java
two ticked -> topics=java&topics=cssWhen should you use a select?
Use it when the list of options is long, or when the options are unlikely to be needed every time.
A list of countries is the classic case. It is ninety options that nobody wants to scroll past.
A select also takes up only one line of the layout, and that matters in a dense toolbar or a narrow column.
<label for="country">Country</label>
<select id="country" name="country">
<option value="" selected disabled>Choose a country</option>
<optgroup label="Europe">
<option value="uk">United Kingdom</option>
<option value="ie">Ireland</option>
</optgroup>
<optgroup label="North America">
<option value="us">United States</option>
</optgroup>
</select>An optgroup groups related options and gives the list some structure, and that helps when there are many.
The disabled placeholder prevents an empty string from being submitted as a choice.
A select can also allow several answers at once, with the multiple attribute. But the interaction changes completely then. The visitor has to hold a modifier key to select more than one, and that is a usability trap on a touch screen.
If you need multiple choices from a long list, use checkboxes instead.
Which control should I use for a given question?
The decision comes down to two things. Is more than one answer allowed? Does the visitor benefit from seeing every option up front?
Working through those two questions usually settles the choice immediately.
question answer allowed options visible use
"which plan?" one yes radio buttons
"which topics?" many yes checkboxes
"which country?" one no (too many) select
"accept the terms?" yes or no n/a one checkbox
"which delivery speed?" one yes radio buttonsTwo patterns come up constantly.
A choice between two options, such as yes and no, or male and female, or on and off, is usually a select or a radio group and not a checkbox. A checkbox implies that not ticking it is a valid third answer.
And when the list of options is long, and the visitor mostly needs one common value, a select with the common choice first is kinder than a wall of checkboxes.
What is the difference between disabled and readonly?
These two attributes look similar, and they behave differently in a way that matters.
A disabled control is not submitted with the form at all. It cannot be focused, and it cannot be edited.
A readonly control is submitted. It can be focused and selected, but it cannot be edited. That is exactly right for a field that the server fills in and that the visitor needs to see.
<!-- not sent with the form at all -->
<input type="text" name="discount" value="SPRING" disabled>
<!-- sent with the form, visible but not editable -->
<input type="text" name="discount" value="SPRING" readonly>For a field that should be hidden from the visitor but still sent to the server, use a hidden input and not a disabled input. A disabled field is silently dropped, and the server never learns what the visitor was shown.
Quick reference
- Radio buttons give one answer from a small visible set, and they are grouped by a shared name.
- Checkboxes allow any number of answers, each one is independent, and they are all submitted together.
- A select gives one answer from a long or rarely used list, and the options stay hidden until they are opened.
- You group the radios and the related checkboxes in a fieldset with a legend.
- You use checked for a sensible default, and a disabled placeholder for the "choose one" instruction.
- You prefer checkboxes over a multi-select, which is painful on touch screens.
Try it yourself: you build the same question three ways, as radio buttons, as a select, and as checkboxes. Then you submit the form to a local server and look at the body of the request for each one. You will notice how the same question produces different data, depending on the control you chose.