Your sign-up form now gives the email, phone, and hours fields types that fit their data, but Task and Time slot are still plain text boxes. Ask three volunteers which task they’d like, and Rosa might get weeding, Weeds!, and the compost thing. There are only three tasks and two time slots, so the form can list them and let the visitor pick.
In this lesson, you’ll turn the task field into a group of choices with a name a screen reader can announce, and the time slot into a dropdown list. You’ll also add a checkbox and a box for a note, submitting as you go to see what each control sends. Start with the task.
Offer One Choice in a Named Group
A text box accepts anything, but the task has exactly three right answers. A radio button is an <input type="radio">, a small circle the visitor selects.
The three radios still need their question, Task, and a label can name only one field. A <fieldset> groups related controls, and a <legend> as its first child names the group. In events/work-day.html, replace the paragraph that holds the task’s label and text box with this fieldset:
<fieldset>
<legend>Task</legend>
<label>
<input type="radio" name="task" value="weeding">
Weeding
</label>
<label>
<input type="radio" name="task" value="watering">
Watering
</label>
<label>
<input type="radio" name="task" value="compost">
Building the new compost bays
</label>
</fieldset>
Each radio sits inside its own label, the wrapping style from the lesson Label Every Field, so clicking the words selects the circle. Radios that share a name form one group, where only one can be selected. Each value is what’s sent when that radio is selected, since the visitor types nothing.
As a programmer, you can read the group as one variable that holds one of three values: name is the variable’s name, and each value is a value it can hold. The comparison breaks down when nothing is selected. A variable would still exist and hold an empty value, but the group sends no pair at all.
Save, and the browser draws a thin border around the group, with the legend set into its top edge: