HTML select dropdown generator with option groups and a preselected value

Type one option per line, start a line with # to begin a group, and mark the default with an asterisk to get a labelled select dropdown with optgroup headings, a placeholder first option and the required or multiple attribute if you want them.

Live demo · real result from this tool

Press Generate to create a result.

Processed on your device — nothing is uploaded.

How to use the select dropdown generator

  1. Type the options in Options, one per line. Write value|label, or only a label to have the value made for you.
  2. Start a line with # to open a group, for example # Europe. End an option with a space and * to preselect it.
  3. Fill in the Label and the name.
  4. Add a First empty option (optional), such as “Choose a country”.
  5. Tick Allow multiple and Required as needed.
  6. Copy the code and open the dropdown in the Preview.

What it does and when to use it

A select lets people choose from a fixed list without typing, which keeps data consistent: every country, plan or size arrives spelled the same way. Long option lists are tedious to write by hand, and small mistakes such as a missing </optgroup> or a duplicated selected are easy to miss. The generator writes the whole control from a plain list.

It always adds a visible label linked with for and id, so screen readers announce what the dropdown is for and a click on the label opens it. For a form with several fields at once, use the HTML Form Generator. For a single text, email or date field, use the HTML Input Generator.

How it works

Each non-empty line is read in order. A line starting with # closes any open group and opens <optgroup label="…">. Lines with | are split into value and label. Lines without it use the text as the label and a slug of it as the value: “Green” becomes green, “New Zealand” becomes new-zealand. A trailing * adds selected. The id comes from the name, or from the label if the name is empty. Text is HTML-escaped. The code is built in your browser.

Worked examples

The sample country list puts India first and preselected inside an Asia group:

<optgroup label="Asia">
  <option value="in" selected>India</option>
  <option value="jp">Japan</option>

The Europe group follows, and the list starts with <option value="">Choose a country</option>.

Colours with multiple selection. Options Red, Green * and Blue, label “Colour”, name “colour”, no placeholder, not required, multiple ticked:

<label for="colour">Colour</label>
<select id="colour" name="colour[]" multiple>
  <option value="red">Red</option>
  <option value="green" selected>Green</option>
  <option value="blue">Blue</option>
</select>

Limits and tips

  • Multiple selects are hard to use on desktop, since people must hold Ctrl or Cmd. For a few choices, checkboxes are clearer.
  • Only one option should be preselected in a single select; if several are marked, browsers show the last one.
  • Values are case-sensitive on most servers. Keep them in one style, such as lower case.
  • A dropdown that only navigates to other pages is better built as links; the HTML Nav Menu Generator makes those.

Frequently asked questions

What is the difference between the value and the label?
The label is what people see in the list; the value is what the form sends. In "in|India", the visitor picks India and the server receives in. Short, stable values such as country codes are easier to store and do not change if you reword the label.
How does the empty first option work with required?
The first option has an empty value. With required on, the browser refuses to submit the form while that option is chosen, so people must make a real choice. Without a placeholder, the first real option is selected by default and may be sent without anyone choosing it.
Why does the name end in [] with multiple selection?
A multiple select sends one value per chosen option, all under the same name. PHP and some other frameworks collect them into a list only when the name ends with []. If your back end does not need it, remove the brackets.
Are option groups accessible?
Yes. Screen readers announce the optgroup label, such as Europe, along with the options in it. Group headings cannot be selected themselves, which suits continent or category names.
When is a select the wrong control?
When there are only two to five choices, radio buttons show all options at once and need fewer clicks. For very long lists such as all countries, consider a text input with a datalist so people can type to filter.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close