CHAPTER 33 · HTML
Choices and groups: radio buttons, checkboxes, fieldset and legend
Some questions need one answer, while others allow several. Choose the right control, give every option a clear label and understand exactly which values a form sends.
Download example files (ZIP)One choice or several independent choices?
Build a radio group
<input type="radio" id="morning" name="session" value="morning">
<label for="morning">Morning practice</label>
<input type="radio" id="afternoon" name="session" value="afternoon">
<label for="afternoon">Afternoon practice</label>Fieldset and legend give the group a question
- 1Fieldset and legend: the shared question
- 2Label and id: one named control
- 3Radio name: one choice group
- 4Checkbox values: independent selections
Unchecked choices do not automatically send false. Give meaningful values and check repeated entries.
Complete program: session-choice.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Choose One Practice Session</title>
</head>
<body>
<h1>Choose a session time</h1>
<form action="form-result.html" method="get">
<fieldset>
<legend>Which practice time suits you?</legend>
<p><input type="radio" id="morning" name="session" value="morning" checked><label for="morning">Morning practice</label></p>
<p><input type="radio" id="afternoon" name="session" value="afternoon"><label for="afternoon">Afternoon practice</label></p>
</fieldset>
<button type="submit">Inspect the chosen time</button>
</form>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. Form submission is disabled in this preview. Save the example and its result page locally to test submission. Use fictional practice values.
Checked means an initial choice
Independent checkbox values
Complete program: supply-choices.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Choose Independent Practice Supplies</title>
</head>
<body>
<h1>Choose what you will bring</h1>
<form action="form-result.html" method="get">
<fieldset>
<legend>Supplies you can bring</legend>
<p><input type="checkbox" id="notebook" name="supplies" value="notebook" checked><label for="notebook">Notebook</label></p>
<p><input type="checkbox" id="pen" name="supplies" value="pen"><label for="pen">Pen</label></p>
<p><input type="checkbox" id="folder" name="supplies" value="folder"><label for="folder">Folder</label></p>
</fieldset>
<p><input type="checkbox" id="practice-note" name="read_note" value="yes"><label for="practice-note">I read the practice instructions</label></p>
<button type="submit">Inspect the selections</button>
</form>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. Form submission is disabled in this preview. Save the example and its result page locally to test submission. Use fictional practice values.
Labels and keyboard use
Disabled groups and required choices
Problem and solution: both radio answers stay selected
Common mistakes and useful tips
Giving each radio answer a different group name. Reusing one id for several controls. Expecting unchecked boxes to send false. Forgetting explicit option values and receiving on instead. Using a paragraph as the only group label when fieldset and legend fit. Treating required on every checkbox as an at-least-one rule. Pretending a practice choice creates a real booking.
Practice questions
Which control fits one workshop time, and which fits several independent supplies? Which attribute connects related radios into one group in the same form? What does an unchecked ordinary checkbox contribute to form data? How do checked and value differ? Does required on three checkboxes mean at least one or all three? Save both examples beside the result page. Select two supplies and Afternoon, inspect the resulting named values and then repeat with all supplies unchecked.
Check your practice answers
Radios for one time; checkboxes for independent supplies. A shared non-empty name, with the same form owner. No entry for that checkbox. Checked supplies an initial selected state. Value identifies the submitted answer when selected. Each required checkbox must itself be checked, so all three. Two supply values appear under the repeated supplies key. The radio contributes session=afternoon. With all supplies unchecked, there are no supplies entries. These demonstrations do not create stored records.
Interview questions
Why do radios share a name but keep unique ids?
Why use fieldset and legend for related choices?
How would you read several checked options with the same name?
Does disabled preserve a value in submission?
Quick revision
Radios choose one answer within a named group. Checkboxes represent independent choices and can submit repeated keys. Use labels for answers and legend for the group's question. Initial selection, value, disabled and required have different jobs.