Read Radio Buttons and Checkbox Groups

In Read a Whole Form With FormData, each form submission became a record: get() read the name, the count, and the aisle, and has() turned the Need it today box into true or false. Every one of those controls holds one value. Some of Sam’s choices don’t fit that shape.

Sam wants to mark how urgent each item is and note which shops sell it. In this lesson, you’ll build controls that share one name, predict what FormData holds for them, and read each kind correctly into the record. You’ll also make the page react the moment Sam selects an option, before anything is submitted. Start with a choice that has exactly one answer.

Offer One Choice With Radio Buttons

An item’s priority is low, normal, or urgent, and never two of those at once. A select control could hold that, but it hides the options until Sam opens it. With three short options, showing them all is friendlier.

A radio button is an <input> with type="radio". Radio buttons that share a name form a radio group, and the browser lets only one of them be checked, so picking one unchecks the others. Add this group to index.html, after the Need it today label and before the Add button:

Language: HTML Filename: kitchen/index.html
<fieldset>
  <legend>Priority</legend>
  <label>
    <input type="radio" name="priority" value="low">
    Low
  </label>
  <label>
    <input type="radio" name="priority" value="normal" checked>
    Normal
  </label>
  <label>
    <input type="radio" name="priority" value="urgent">
    Urgent
  </label>
</fieldset>

Each radio sits inside its own <label>, like the checkbox, so clicking the word picks the button. The <fieldset> element draws a box around the group, and its <legend> gives the whole group a name. A screen reader announces Priority along with each button, so the three words make sense on their own.

The checked attribute makes Normal the starting choice. Without it, no radio is checked until Sam clicks one, and an unchecked group sends nothing, as an unticked checkbox does. With it, every item has a priority, and form.reset() puts Normal back after you add each item.

Since the group sends one value under one name, you read it the way you read the aisle. Add this line to the record in your submit listener, after today:

Language: JavaScript Filename: kitchen/main.js
      priority: data.get("priority"),

The get() call returns the value of whichever radio is checked, so the record gets "low", "normal", or "urgent". Save, and try the group from the keyboard too. Press Tab until the focus reaches Normal, then press Right. Urgent becomes the checked button because the arrow keys move the choice within a group.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.