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?

Imagine choosing a workshop time: morning or afternoon, but not both. That is a radio group. Now imagine choosing supplies to bring: a notebook, a pen and a folder can each be selected independently. Those are checkboxes.

The shape of the control is a clue, but the question's meaning decides which kind to use. Do not use checkboxes and then merely write “Choose one” when the actual interface allows several answers.

Build a radio group

HTML fragment — part of a document
<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>

Within the same form, radios with the same non-empty name form one group. Each has a unique id for its label and a distinct value describing its answer. Selecting one option deselects the other in that group. Radios in separate forms are not combined merely because they share a name.

Name is the question's data key; value is the chosen answer. If Afternoon practice is selected, the pair is session=afternoon, not session=Afternoon practice unless you deliberately use those words as the value.

Fieldset and legend give the group a question

Fieldset groups related controls. Legend supplies its caption and normally goes first inside fieldset. A paragraph that happens to sit nearby does not create the same grouping relationship.

For a question with several options, a useful legend names the whole question, while each label names one answer. Do not put a separate fieldset around every individual radio and lose the relationship among them.

Question, options and submitted answers
  1. 1Fieldset and legend: the shared question
  2. 2Label and id: one named control
  3. 3Radio name: one choice group
  4. 4Checkbox values: independent selections

Unchecked choices do not automatically send false. Give meaningful values and check repeated entries.

Complete program: session-choice.html

Save this beside form-result.html from Chapter 31. Test actual submission in your own browser; the embedded preview does not submit forms.

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>
Browser previewDocument title: Choose One Practice Session

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.

Expected result: Morning starts selected. Choose Afternoon and observe that Morning clears. In the saved program, submit and inspect session=afternoon in the query. The static result page still does not reserve a seat.

Checked means an initial choice

Checked is a boolean attribute selecting the initial state of a radio or checkbox. Checked="false" still includes it; remove checked to start unselected. It is different from value, which identifies what the option contributes when selected.

Write at most one initial checked choice per radio group. Give both options labels and make sure the default choice is actually appropriate. Some questions should start with no selection so the reader makes a deliberate choice.

Independent checkbox values

Checkboxes can share a name when their selections form a list. Selecting two can produce repeated keys such as supplies=notebook and supplies=pen. A receiving application must read all intended values, rather than silently keeping only one.

If an enabled checkbox or radio is not checked, it normally contributes no entry. It does not automatically send false. If a checked checkbox has no explicit value, its ordinary default submitted value is on, which is often less useful than a deliberately named value.

Complete program: supply-choices.html

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>
Browser previewDocument title: Choose Independent Practice Supplies

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.

Expected result: several supply choices can stay selected together. Check Pen without clearing Notebook, then inspect the repeated supplies keys in a local submission. Leave read_note unchecked and observe that its name is absent rather than equal to false.

Labels and keyboard use

A linked label gives the control a name and increases its usable activation area. Clicking a checkbox label normally toggles the checkbox. A radio label normally selects that radio. You may also wrap the input in its label, but keep the relationship clear and do not nest interactive links inside labels unnecessarily.

Keyboard users commonly reach a radio group with Tab and move among its options with arrow keys; details can vary by browser and initial selection. Space normally toggles a focused checkbox. Test native controls rather than replacing them with decorative shapes that lack those behaviours.

Disabled groups and required choices

Disabled makes a supported control unavailable and excludes it from submitted data. Disabled on a fieldset also disables its descendant controls, except controls within its first legend. That exception is one reason to keep legends simple while learning.

Required on a radio in a named group means an option in that group must be selected for ordinary validated submission. Required on a checkbox applies to that individual checkbox. Adding required to three independent checkboxes requires all three; it does not mean choose at least one. A group-wide “at least one checkbox” rule needs additional logic or a different interface.

Problem and solution: both radio answers stay selected

Problem: the morning control has name="morning" and the afternoon control has name="afternoon". Both can remain selected because they are separate groups.

Solution: use the same name for one question and distinct values for its answers. Keep separate ids for labels. Do not make the ids identical just because the radios belong together.

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

  1. Which control fits one workshop time, and which fits several independent supplies?
  2. Which attribute connects related radios into one group in the same form?
  3. What does an unchecked ordinary checkbox contribute to form data?
  4. How do checked and value differ?
  5. Does required on three checkboxes mean at least one or all three?
  6. 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
  1. Radios for one time; checkboxes for independent supplies.
  2. A shared non-empty name, with the same form owner.
  3. No entry for that checkbox.
  4. Checked supplies an initial selected state. Value identifies the submitted answer when selected.
  5. Each required checkbox must itself be checked, so all three.
  6. 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?

Name identifies the submitted question and its group. Id identifies one document element so its label can connect correctly. The two attributes serve different purposes.

Why use fieldset and legend for related choices?

They describe a group and its shared question, helping readers understand the individual option labels in context. They are more than a decorative border and title.

How would you read several checked options with the same name?

Treat them as potentially repeated entries for that name. The receiving code must use an appropriate all-values operation rather than assume every name has just one value.

Does disabled preserve a value in submission?

No. Disabled controls are excluded from ordinary form data. If a real service needs a trusted value, it must obtain or verify it through appropriate receiving-side logic.

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.