HTML Select Lists

This page contains HTML code for creating a select list. You can copy/paste this code into your own blog or website in order to create your own select list.

A select list is a form element that allows the user to select one or more options from a range of options.

Select lists are created using a combination of the HTML <select> and <option> tags. Select lists can be nested inside a <form> element or they can stand alone. They can also be associated with a form via the form attribute of the <select> tag.

Basic Select List Example

Here's an example of an HTML select list:

Source CodeResult

Working Example

Here, we add a submit button so that the user's selection can be submitted and processed. We also place the select list within a <form> element that specifies a page to process the form.

Source CodeResult

What would you like for lunch?

Result:

Multiple Options

A select list can allow multiple options to be displayed at the one time. It can also allow multiple options to be selected by the user.

Source CodeResult

What would you like for lunch?

Result:

Multiple Selections

The user can select more than one option on these types of lists. For example, on a Mac, they could hold the command key down, while clicking on their multiple selections. On a PC, they could hold the Ctrl key down while making their selection. Users could also hold the Shift key down (but this won't work on non-contiguous files). You could select all files by clicking in the select area and doing a "select all" (command | A on a Mac, Ctrl | A on a PC)

The form Attribute

In the above example, we enclosed the select list inside a form. HTML5 also allows you to specify a form using the form attribute. This method could be used in the event that the select list is not inside the form that needs to submit its contents.

In the next example, add id="myForm" to the form. We then add form="myForm" to the select list and move the select list outside of the form. Submitting the form should still submit the contents of the select list, because the select list references the id of the form in its form attribute.

Source CodeResult

What would you like for lunch?

Result:

Set a Default Value

You can set a default value by using the selected attribute against an option. When this is added to a <option> tag, that option is selected as soon as the page loads (i.e. it doesn't need the user to select it). The user can then choose to "deselect" that option if so desired, but if they don't it will remain selected.

In this example, we've checked the first option.

Source CodeResult

What would you like for lunch?

Disabling an Option

You can disable an option by using the disabled attribute. You could use this in conjunction with a script to enable/disable the option depending on whether certain criteria has been met.

In this example, we've disabled the third option (Lychee).

Source CodeResult

Select Lists vs Radio Buttons & Checkboxes

Select lists provide similar functionality to radio buttons in that they allow the user to select one option from a list of options. Multi-select lists are more like checkboxes, in that they allow the user to select any number of options.

Given that select lists provide similar functionality to radio buttons and checkboxes, this raises the question:

When should you use a select list instead of radio buttons or checkboxes?

Here are some considerations.

Hopefully these factors will help you decide which option to use when designing your forms.