Skip to content
Tutorials Center
My learning

Topics

Lesson 67 of 122 Β· Forms

Drop-downs and Suggestions

Build drop-down menus with select, option and optgroup, allow multiple choices, add a required placeholder option, and offer typing suggestions with datalist.

When a list of choices grows beyond a handful, radio buttons start to take over the page. A drop-down packs the same choices into a single compact control. HTML gives you two tools for this: <select>, which limits people to the options you provide, and <datalist>, which suggests options but still lets people type anything they like.

In this lesson you will build both, learn how they submit their values, and find out when to use which.

The <select> element

A <select> contains a list of <option> elements. Like every control, it needs a name and a label:

HTML
<label for="size">T-shirt size</label>
<select id="size" name="size">
  <option value="s">Small</option>
  <option value="m">Medium</option>
  <option value="l">Large</option>
  <option value="xl">Extra large</option>
</select>

Each option has two parts:

  • The text between the tags is what people see: "Medium".
  • The value is what is submitted: size=m.

If an option has no value attribute, its text is submitted instead. That is fine for simple lists, but an explicit value keeps your server code stable when you later reword or translate the visible text.

Which option starts selected?

A plain drop-down always has a selection. By default it is the first option. Add the boolean selected attribute to start on a different one:

HTML
<option value="m" selected>Medium</option>

Just like checked on a checkbox, selected only sets the starting state. The live choice is available in JavaScript as select.value.

Grouping options with <optgroup>

Long lists are easier to scan when related options are grouped. Wrap them in <optgroup> with a label attribute. The group label is shown as a heading in the list, but it cannot be selected:

Select with groups and disabled options

Notice three things: Seoul starts selected, the submitted value is the airport code rather than the city name, and Kathmandu is visible but cannot be picked. disabled works on single options and on whole <optgroup> elements.

Optgroups cannot be nested. If your data has more than two levels, consider splitting it into two drop-downs, or a different kind of control.

A "please choose" placeholder

Often you do not want to pick a default for people. Maybe there is no sensible one, or you want to be sure they made a conscious choice. The pattern is an empty first option plus required:

HTML
<label for="country">Country</label>
<select id="country" name="country" required>
  <option value="">Choose a country…</option>
  <option value="ca">Canada</option>
  <option value="jp">Japan</option>
  <option value="ng">Nigeria</option>
</select>

HTML calls that first option a placeholder label option. For it to count, the option must be first, have an empty value, and the select must be required, not multiple, and show one row at a time. If the person leaves it selected, the browser refuses to submit and asks them to choose. You will see more of this behavior in Form Validation.

Multiple selections

The boolean multiple attribute turns the drop-down into a list box that allows several selections. The size attribute sets how many rows are visible at once:

HTML
<select id="languages" name="languages" multiple size="5">
  ...
</select>

Each selected option is submitted as its own pair, exactly like a group of checkboxes with the same name: languages=fr, languages=pt, and so on.

Multiple selections

Multi-select list boxes work, but they have a usability problem: many people do not know about holding Ctrl or Cmd, and one careless click can wipe out a long selection. For short lists, a group of checkboxes (see Checkboxes and Radio Buttons) is usually friendlier. If you do use multiple, always explain how to use it, as the hint above does.

Suggestions with <datalist>

Sometimes you want to suggest common answers without forcing one of them. A <datalist> holds a list of options, and an input's list attribute points to it by id:

HTML
<label for="browser">Favorite browser</label>
<input type="text" id="browser" name="browser" list="browser-options">

<datalist id="browser-options">
  <option value="Chrome"></option>
  <option value="Edge"></option>
  <option value="Firefox"></option>
  <option value="Safari"></option>
</datalist>

As someone types, the browser shows matching suggestions under the input. They can pick one, or ignore the list and type something else entirely. Whatever ends up in the box is submitted: there is nothing special about a value that came from the list.

A datalist on its own displays nothing. It only appears through an input that refers to it, and several inputs can share the same list.

Datalist suggestions

list works with more than text boxes. On a range input, as in the example, many browsers draw the datalist values as tick marks along the slider. It is also supported on email, url, tel, search, number, color and the date and time types, though how suggestions look varies a lot between browsers. On password, checkbox, radio, file, hidden and the button types, list is ignored.

Datalist is a hint, not a rule. Never assume a submitted value is one of your suggestions. If the answer must come from a fixed list, use <select> (and still check it on the server).

Select, radio buttons or datalist?

Situation Best control
Pick one of a few options (about 2–7), all worth seeing at once Radio buttons
Pick one of many options, such as a country or time zone <select>
Pick several options from a short list Checkboxes
Pick several from a long list <select multiple>, with instructions
Free text, but with common answers worth suggesting <input> with <datalist>

Drop-downs hide their options until opened, so for a very small set of choices, radio buttons are faster to read and need fewer clicks. For a very long list (hundreds of items), even a select gets tiresome; a text input with suggestions can be quicker.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. An option is written as <option value="pt">Portugal</option>. What is submitted when it is chosen?

    The value attribute is what is submitted. The text between the tags is only for display, and is used only when there is no value.
  2. 2. How do you make a select show a "Choose…" option that people are not allowed to submit?

    An empty-valued first option on a required, single-row select is a placeholder label option. Submitting with it selected fails validation. <select> has no placeholder attribute.
  3. 3. A text input is linked to a datalist of five cities. Someone types a sixth city that is not in the list. What happens?

    A datalist only suggests values. Whatever is in the input is submitted.
  4. 4. What is <optgroup> for?

    An optgroup's label is shown as a heading in the list. Multiple selection comes from the multiple attribute.

Summary

  • <select> holds <option> elements; the option's value is submitted, and its text is shown.
  • selected sets the starting choice, disabled blocks an option or a whole <optgroup>.
  • An empty first option plus required forces people to make a real choice.
  • multiple and size create a list box that submits one pair per selected option; explain how to use it, or prefer checkboxes.
  • <datalist> suggests values for an input through its list attribute, but any value can still be submitted.

Next up: Text Areas and Buttons, for longer text and for the buttons that send it.