Back to HTML

Select Dropdowns, optgroup and datalist

Coding Tutorials Team 30 June 2026

The select element presents a list of options behind a single control. It is one of the oldest form controls and remains the right answer for long lists, though it is frequently used where something simpler would serve better.

The basic structure

<label for="country">Country</label> <select id="country" name="country"> <option value="gb">United Kingdom</option> <option value="ie">Ireland</option> <option value="fr">France</option> </select>

The value attribute is what gets submitted. Where it is omitted, the text content of the option is submitted instead, which works but couples your data to your display text and breaks the moment someone rewords a label.

Preselecting and prompting

The selected attribute chooses an initial option. Without it, the first option is selected, which is why a dropdown that appears to default to a real answer is often submitting one the user never chose.

The usual fix is a prompt option at the top with an empty value, combined with the required attribute so an empty submission is rejected.

<option value="" selected disabled>Choose a country</option>

Marking it disabled prevents the user returning to it once they have chosen, which is usually desirable on a required field.

Grouping options

The optgroup element groups related options under a heading, which makes a long list considerably easier to scan.

<select id="topic" name="topic"> <optgroup label="Front end"> <option value="html">HTML</option> <option value="css">CSS</option> </optgroup> <optgroup label="Back end"> <option value="php">PHP</option> <option value="sql">SQL</option> </optgroup> </select>

The group label is a heading rather than a selectable option, and groups cannot be nested. One level is all the element supports.

Multiple selection

The multiple attribute turns the control into a list box allowing several selections, and the size attribute controls how many rows are visible.

In practice this is a poor interface. Selecting multiple items requires holding a modifier key, which is not discoverable and does not exist on touchscreens. A group of checkboxes achieves the same result with an interaction everyone understands, and is almost always the better choice.

Disabling options

Individual options can be disabled, which greys them out and prevents selection while leaving them visible. This is useful for showing that a choice exists but is currently unavailable, such as a delivery slot that is fully booked.

An entire group can be disabled by putting the attribute on the optgroup, which disables every option inside it. Where a whole select is disabled, remember that disabled controls are not submitted at all, so the server receives nothing rather than the previously selected value.

Reading the value

The submitted value is the value attribute of the selected option, and only one is sent unless the control allows multiple selections. In JavaScript, the select element exposes both the chosen value directly and the full list of options, which is what makes dependent dropdowns straightforward: choosing a country repopulates a region list without any page reload.

When building those dependencies, keep the second control disabled until the first has been chosen, and repopulate rather than hide options, since hidden options behave inconsistently across browsers.

When a select is the wrong control

For fewer than about five options, radio buttons show everything at once and need one tap rather than two. For a yes or no question, a single checkbox is simpler than a two option dropdown.

For very long lists, a plain select becomes tedious to scroll. A searchable control is genuinely better there, though building one that remains keyboard accessible and works with assistive technology is more work than it appears, and the native control has all of that behaviour already.

The datalist alternative

A datalist attaches a list of suggestions to an ordinary text input. The user may pick one or type something else entirely.

<label for="framework">Framework</label> <input list="frameworks" id="framework" name="framework"> <datalist id="frameworks"> <option value="React"> <option value="Vue"> <option value="Svelte"> </datalist>

The list attribute on the input references the datalist by id. This is the right control when your options are suggestions rather than an exhaustive set, and it filters as the user types, which a select does not.

Two caveats. The value is whatever was typed, so the server must handle unexpected input. And the rendering and keyboard behaviour vary between browsers more than most native controls, so test it rather than assuming.

Styling limitations

The closed select can be styled reasonably well, including with the appearance property to remove the default arrow and supply your own. The open dropdown list is rendered by the operating system and has historically been almost entirely outside your control.

This is the usual reason projects replace the native control with a scripted one. Before doing so, consider what the native version provides at no cost: full keyboard support, type-ahead selection, correct announcement by screen readers, and a familiar mobile interface that takes over the screen and is genuinely easier to use than most replacements.