Lesson 37 of 51 · Module 6, lesson 3 of 6 · 3 min

Dropdowns, text areas and grouped fields

Offer choices with dropdowns and radio buttons, collect longer messages, and group related fields.

What you'll learn

  • How to offer a choice with <select> and with radio buttons
  • How to collect a longer message with <textarea>
  • How to group related fields with <fieldset> and <legend>

Why this matters

Many questions have a fixed set of answers: a size, a day, yes or no. Others need a few sentences. Using the right control makes the form quicker to fill in. And grouping related options tells screen reader users what question the options answer.

A dropdown: <select>

html
<label for="level">Your cooking level</label>
<select id="level" name="level">
  <option value="">Choose one</option>
  <option value="beginner">Beginner</option>
  <option value="some">Some experience</option>
  <option value="expert">Expert</option>
</select>

Each <option> has a value (what is sent) and text (what the visitor sees). The first empty option makes the visitor choose on purpose.

A longer message: <textarea>

html
<label for="message">Your message</label>
<textarea id="message" name="message" rows="4"></textarea>

<textarea> has a closing tag. rows sets how many lines are visible. Anything between the tags appears as starting text — usually leave it empty.

Radio buttons in a group

Radio buttons let the visitor choose exactly one option. Every button in a group has the same name; that is what links them. Wrap the group in <fieldset>, and give it a <legend> — the question:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Class feedback</title>
</head>
<body>
  <h1>Class feedback</h1>
  <form>
    <fieldset>
      <legend>Which class did you take?</legend>
      <p>
        <input id="soup" name="class" type="radio" value="soup">
        <label for="soup">Soups</label>
      </p>
      <p>
        <input id="bread" name="class" type="radio" value="bread">
        <label for="bread">Bread</label>
      </p>
    </fieldset>
    <p>
      <label for="level">Your cooking level</label>
      <select id="level" name="level">
        <option value="">Choose one</option>
        <option value="beginner">Beginner</option>
        <option value="some">Some experience</option>
        <option value="expert">Expert</option>
      </select>
    </p>
    <p>
      <label for="message">What did you enjoy?</label><br>
      <textarea id="message" name="message" rows="4" cols="30"></textarea>
    </p>
    <p><button type="submit">Send feedback</button></p>
  </form>
</body>
</html>

What you'll see: a box drawn around the radio buttons, with the question on its border; a dropdown; a larger text box; and a button.

The "Class feedback" form: a bordered group titled "Which class did you take?" with two radio buttons, Soups and Bread; a dropdown labelled "Your cooking level" showing "Choose one"; a multi-line box labelled "What did you enjoy?"; and a "Send feedback" button.

A screen reader now says, for example: "Which class did you take?, group. Soups, radio button, 1 of 2." Without the <fieldset> and <legend>, the listener would hear only "Soups" — with no question.

Checkboxes can be grouped too

For "choose any that apply", use checkboxes in a <fieldset>. Give each its own name, or the same name for all — a server can handle either.

Suggestions: <datalist>

<datalist> gives a text box a list of suggestions, while still allowing any answer:

html
<label for="dish">Favourite dish</label>
<input id="dish" name="dish" list="dishes">
<datalist id="dishes">
  <option value="Tomato soup">
  <option value="Green salad">
</datalist>

Radio buttons or a dropdown?

  • 2–5 options: radio buttons — all choices are visible at once.
  • Many options (such as a list of countries): a <select>.

Try it yourself

  1. Select the feedback example and choose Try it.
  2. Add a third class, "Salads", to the radio group. Check that choosing it removes the other choice.
  3. Give it a different name by mistake. Now you can select two options — the group is broken. Fix it.
  4. Submit the form and read which values were sent.

Common mistakes

  • Radio buttons with different name values. They no longer work as a group. Fix: one name for the whole group.
  • No value on radio buttons. The form sends class=on. Fix: give each a clear value.
  • Radio or checkbox groups without <fieldset> and <legend>. Screen reader users do not hear the question. Fix: wrap every group.
  • A self-closing <textarea />. The rest of the page ends up inside the box. Fix: always <textarea></textarea>.

Check yourself

  1. What makes several radio buttons one group?
  2. Which element holds the question for a group of radio buttons?
  3. For a choice between 40 countries: radio buttons or <select>?

Answers

  1. The same name value.
  2. <legend>, inside <fieldset>.
  3. <select>.

Next: every form needs a button — and there is more to buttons than it seems.

Chat with us on WhatsApp