HTML <select> Tag

The <select> tag is used to create a dropdown list, also known as a select box or combo box. This allows users to choose one or more options from a predefined list. It's a crucial element for building interactive forms in HTML.

HTML Select Tag: Create Dropdown Lists

Syntax

<select name="selectName"
        autocomplete="on|off"
        autofocus
        disabled
        form="form_id"
        multiple
        name="name"
        required
        size="number">
    <optgroup label="groupName">
       <option value="optionValue" selected>Option Text</option>
       ...
    </optgroup>
    <option value="optionValue" selected>Option Text</option>
    ...
</select>

Attributes

Attribute Value Description
autocomplete on \ off Specifies whether the browser should automatically complete the form (values).
autofocus autofocus Specifies that the dropdown should automatically get focus when the page loads. Only one element can have this attribute per page.
disabled disabled Specifies that the dropdown should be disabled (unusable).
form form_id Specifies the form the dropdown belongs to. The value of this attribute must match the ID of the form.
multiple multiple Specifies that multiple options can be selected.
name name Specifies the name of the dropdown, used when submitting form data.
required required Specifies that the user is required to select a value before submitting the form.
size number Defines the number of visible options in a scrollable list.

Example

<form action="/submit" method="post">
  <label for="cars">Choose a car:</label>
  <select name="cars" id="cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </select>
  <br><br>
  <input type="submit" value="Submit">
</form>

More Examples

Basic Select with Default Option

<select name="country">
  <option value="us">United States</option>
  <option value="ca" selected>Canada</option>
  <option value="uk">United Kingdom</option>
</select>

Explanation: The selected attribute sets "Canada" as the default selected option.

Select with Optgroups

<select name="city">
  <optgroup label="North America">
    <option value="ny">New York</option>
    <option value="la">Los Angeles</option>
    <option value="tor">Toronto</option>
  </optgroup>
  <optgroup label="Europe">
    <option value="ldn">London</option>
    <option value="par">Paris</option>
    <option value="ber">Berlin</option>
  </optgroup>
</select>

Explanation: optgroup elements group related options, improving readability and organization within the dropdown.

Multiple Selection

<select name="fruits" multiple size="3">
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="orange">Orange</option>
  <option value="grape">Grape</option>
</select>

Explanation: The multiple attribute allows users to select multiple options. The size attribute specifies the number of options to be visible without scrolling.

Select with Required field

<form action="/submit" method="post">
  <label for="pet">Choose a pet:</label>
  <select name="pet" id="pet" required>
    <option value="" disabled selected>Select a pet</option>
    <option value="dog">Dog</option>
    <option value="cat">Cat</option>
    <option value="bird">Bird</option>
  </select>
  <br><br>
  <input type="submit" value="Submit">
</form>

Explanation: The required attribute ensures that a user makes a selection before submitting the form. The first option is set as default and disabled, forcing the user to select other value.

Dynamic Options using JavaScript

<select id="dynamicOptions"></select>

<script>
  const selectElement = document.getElementById('dynamicOptions');
  const options = ['Option 1', 'Option 2', 'Option 3'];

  options.forEach(option => {
    const newOption = document.createElement('option');
    newOption.value = option.toLowerCase().replace(' ', '_');
    newOption.textContent = option;
    selectElement.appendChild(newOption);
  });
</script>

Explanation: This example demonstrates adding options to the select element dynamically using JavaScript.

Browser Support

The <select> tag is supported by all modern browsers:

  • Chrome
  • Edge
  • Firefox
  • Safari
  • Opera

Notes and Tips

  • Always include a label with the <label> tag for accessibility purposes.
  • The name attribute is crucial for form submission; make sure it is descriptive and unique within the form.
  • Use optgroup elements to group related options in a large select list for better organization and user experience.
  • Use a placeholder option as the first one with disabled and selected attributes as a default guide, especially if you want the user to select a value, it's good practice to have one with descriptive text such as "Select a value".
  • The size attribute in a multiple select element can significantly improve the user experience by showing more options at once, reducing the need for scrolling in some cases.
  • You can customize the appearance of the select element using CSS, but browser-specific limitations exist. Be aware of browser inconsistencies.
  • When handling form submission, ensure that your backend code is prepared to process the selected values correctly.