Skip to main content

HTML Tags

HTML <select> Tag

Written by Updated HTML <select> Tag exercises

The select tag makes a drop-down list: click it, pick one option, done. It is the right choice when there are more than a handful of fixed options and users must choose one of them, like a country or a size.

Each choice is an option, and options can be grouped under headings with optgroup. For a short list of two to five choices, radio buttons are usually easier, because people can see every option without clicking.

Syntax

html

<select name="dropdownName" id="dropdownId" multiple size="4" required>
  <option value="value1">Option 1</option>
  <option value="value2">Option 2</option>
</select>
AttributeWhat it does
nameThe name the chosen value is sent under.
idConnects it to a <label for="...">.
multipleLets the user pick several options, with Ctrl or Cmd.
sizeHow many options are visible at once, turning it into a list box.
requiredThe form cannot be sent until a real option is chosen.
disabledGreys it out; its value is not sent.
formThe id of a form it belongs to, if it sits outside.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Select Example</title>
</head>
<body>
  <h2>Select Your Favorite Fruit</h2>
  <form action="/samples/form_processor.php" method="post">
    <label for="fruits">Choose a fruit:</label><br>
    <select name="fruits" id="fruits" required>
      <option value="">--Select--</option>
      <option value="apple">Apple</option>
      <option value="banana">Banana</option>
      <option value="orange">Orange</option>
      <option value="mango">Mango</option>
    </select><br><br>

    <input type="submit" value="Submit">
  </form>
</body>
</html>

A drop-down with five fruits appears. The first option, "--Select--", has an empty value, and because the select is required, the form refuses to submit until a real fruit is chosen.

Getting the chosen value in JavaScript

Reacting to a choice

html

<select id="size">
  <option value="s">Small</option>
  <option value="m">Medium</option>
</select>

<script>
  document.getElementById("size").addEventListener("change", (e) => {
    console.log(e.target.value);  // "s" or "m"
  });
</script>

The change event fires when the user picks a different option, and value gives the chosen option's value.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • Always add a <label>. A drop-down with no label is a mystery box for screen reader users.
  • multiple select boxes are hard to use, especially on phones. A group of checkboxes is usually friendlier.
  • Styling the drop-down list itself is limited in most browsers. The newer customizable select, with appearance: base-select and selectedcontent, is changing that in Chromium browsers.

HTML <select> Tag exercises

Add a dropdown

Add a dropdown named colour with two choices: Red, which sends red, and Blue, which sends blue.

Result

Check your answer to see it here.