Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Using the Select Element for Dropdown Menus | HTML Forms and User Input
Ultimate HTML

book
Using the Select Element for Dropdown Menus

The <select> element is used to create a dropdown list of options for the user to choose from. It is typically used in forms where the user needs to choose from a predefined set of options. It is always used in conjunction with <option> elements. The <option> elements define the individual items in the dropdown list.

html

index.html

copy
<form onsubmit="return false">
<select>
<option value="tomato">tomato</option>
<option value="cucumber">cucumber</option>
<option value="lettuce">lettuce</option>
</select>
<button type="submit">Submit</button>
</form>

The <select> element also has several attributes that can be used to modify its behavior, including:

  • name: the name of the select element, used when submitting the form;
  • size: the number of visible options in the dropdown list;
  • multiple: if present, allows the user to select multiple options;
  • disabled: if present, disables the select element.

To divide a list into separate, unrelated groups, you can use the <optgroup> tag. The label attribute is used to assign a title to the group.

html

index.html

copy
<form onsubmit="return false">
<label for="sport">Favorite Sport</label>
<select name="sport" id="sport">
<optgroup label="Team sport">
<option value="soccer">soccer</option>
<option value="basketball">basketball</option>
<option value="hockey">hockey</option>
</optgroup>

<optgroup label="Individual sport">
<option value="boxing">boxing</option>
<option value="swimming">swimming</option>
<option value="skiing">skiing</option>
</optgroup>
</select>
<button type="submit">Submit</button>
</form>

Note

By default, the first <option> is chosen in the list. If you want to change it, you can provide the selected attribute to any <option> element.

question mark

What is the main function of the <select> element in HTML?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 8
some-alt