# Checkboxes, Radios & Pickers

> Checkboxes let users pick any combination of options independently. Radio buttons enforce exactly one choice per named group. Both need a fieldset and legend when the group has a shared question that gives the choices their meaning.

Subject: HTML | Lesson 27 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/checkbox-radio-and-pickers
Previous: [Labels & Text Inputs](https://codewithanimation.in/html/labels-and-text-inputs.md) | Next: [select, textarea & buttons](https://codewithanimation.in/html/select-textarea-and-buttons.md)

## What it is

`<input type="checkbox">` creates an independent toggle. Each checkbox in a form has its own checked state and each contributes a separate entry to the submission when checked. Multiple checkboxes can share a `name` attribute, which causes the submission to include one `name=value` pair per checked box. A checkbox that is not checked contributes nothing at all: the server cannot distinguish "user did not check it" from "user never saw it" without a hidden field carrying a default.

`<input type="radio">` works differently: radios are grouped by their `name` attribute, and the browser enforces that only one per group is checked at a time. Selecting a radio automatically unchecks every other radio with the same `name`, regardless of where they are in the DOM. Unlike checkboxes, a radio group can have at most one value in the submission. The `value` attribute on each radio is what gets sent when that option is selected; if `value` is omitted the browser submits the literal string "on".

## Why it matters

Choosing the wrong control sends the wrong signal to the user. When only one answer is valid ("which plan do you want?"), radio buttons make the mutual exclusion visible and enforce it natively. Using checkboxes for a single-answer question lets the user check multiple boxes and submit an invalid combination that your server then has to reject. The control type is part of the interface contract.

The `<fieldset>` and `<legend>` pattern matters for radio groups in particular. A screen reader navigating to a radio button for "Medium" has no way to know the question is "What size?" unless there is a programmatic group label. `<legend>` provides that label and is announced before each radio in the group when AT reads the options. Removing the fieldset leaves users with a list of options and no question.

## How it works

For a checkbox group, give each `<input type="checkbox">` a unique `id` (for its label) and the same `name` as its siblings. Set `value` to the string you want the server to receive for that option. Each checked box contributes `name=value` to the submission independently. Unchecked boxes contribute nothing.

For a radio group, give every `<input type="radio">` the same `name`. This is what links them: changing one unchecks the others with the same name, wherever they live in the document. Set a distinct `value` on each. Use `checked` on one radio to set the default selection; a radio group without a default forces the user to make a choice before any value is selected, which is sometimes intentional but requires careful validation.

Picker inputs follow the same label and name rules as text inputs. `<input type="date">` opens a calendar picker in supporting browsers and submits a value in `YYYY-MM-DD` format regardless of how the picker displays the date locally. `<input type="color">` submits a lowercase hex string like `#ff5500`. `<input type="range">` submits its numeric value; pair it with an `<output>` element to display the current value.

## Common mistake: Omitting value on radio buttons

A radio button without a `value` attribute submits the string "on" when selected. Every radio in the group then submits "on", making the server-side data useless. This is especially easy to miss during prototyping when radios are added quickly. Always set `value` to the meaningful string the server should receive, even if it duplicates the visible label.

## Tip: Wrap every radio group in a fieldset with a legend

The `<fieldset>` element groups the controls; the `<legend>` element provides their shared label. A screen reader reading the "Medium" radio button will first announce the legend ("Delivery speed") and then the option ("Medium"). Without the legend, the user hears "Medium" with no context. The fieldset also provides a styling hook: browsers apply a default border that visually groups the options, which you can reset with CSS if needed.

## Try it: Try it

Toggle between checkbox and radio modes. Check options and watch the submission preview update to show exactly what the server would receive.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/checkbox-radio-and-pickers

## Check yourself

### Question 1

Three radio buttons in a form have the same name but are in three different <div> containers. How many can be selected at once?

- A. One. Radio buttons are grouped by name, not DOM structure
- B. All three, because they are in separate containers
- C. One per container, so up to three

Correct answer: A

Radio buttons with the same `name` form one group. Only one in the group can be checked at a time, no matter how far apart they are in the HTML.

- Why not B: Radio grouping is determined by the name attribute, not by any enclosing element. Separate containers have no effect on mutual exclusion.
- Why not C: Containers are irrelevant. The browser links all inputs with the same name into one exclusive group. Only one can be checked regardless of nesting.

### Question 2

A radio button has no value attribute. What string does the browser submit when it is selected?

- A. "on"
- B. The text content of its label
- C. An empty string

Correct answer: A

The HTML specification defines the default value of a radio or checkbox as `"on"` when the `value` attribute is omitted. Always set a meaningful `value` string.

- Why not B: The browser submits the value attribute, not the label text. Without a value attribute it falls back to the string "on".
- Why not C: An empty value attribute would submit an empty string, but omitting the attribute entirely causes the browser to use the default fallback: "on".

### Question 3

What does the <legend> inside a <fieldset> provide for a radio group?

- A. The group label announced by screen readers before each option
- B. A visual border around the group
- C. A required field indicator for the group

Correct answer: A

`<legend>` is the accessible name of the group. Screen readers announce it before each radio's own label, giving the user the question ("Delivery speed") alongside each option ("Standard", "Express").

- Why not B: The visual border comes from <fieldset> itself (as a default browser style). <legend> provides the group's accessible name, which screen readers prepend to each control's label.
- Why not C: <legend> is purely a labelling element. Required state must be set with the required attribute on the individual inputs (or handled via ARIA).

## Remember this

- Checkbox: any combination of options can be checked; each checked box submits its own `name=value` pair.
- Radio: only one per `name` group can be checked; grouping is by `name` attribute, not by DOM nesting.
- Always set `value` on radios: omitting it submits the useless string "on".
- Wrap radio groups in `<fieldset>` + `<legend>` so screen readers announce the group question before each option.
