Questions tagged [html-select]

An HTML user interface element for choosing one or more option(s) from a finite collection of options.

The selection (<select>) HTML element represents a user interface control that presents a menu of options. The options within the menu are represented by <option> elements, whose submitted values are determined by its value attribute.

<select name="myselect">
  <option value="some-text">Some Text</option>
</select>

Single versus Multiple Selection Menus

Single-selection menus are the default menus that you find much more option. Users are only allowed to select one option from the drop-down list of options. The control's size can also be changed to accommodate for a longer menu which scrolls through the options rather than creating a drop-down of the options. Any time a size of 2 or greater is used, the menu will become a scrollable box instead. The size specified will include that number of options in the viewable area of the selection menu (so defining a size of 4 will display 4 options high, with a scrollbar). To set the size, simply add size="4" to the <select> element, specifying whatever size you desire.

Multiple-selection menus should always have a size of at least 2, as specifying its size at 1 will cause browser rendering issues (it won't create a scrollbar). Since multiple options can be selected, these menus cannot utilize the drop-down feature. By default, when a select menu is made into a multiple-selection menu, it inherits a size of 4.

The Syntax for Multiple-Selection

<select multiple="multiple">...</select>

Note: While most browsers will still render the menu correctly by only using multiple, it is considered invalid syntax and you should always use the full multiple="multiple" format when setting a menu to multiple-selection. There is no other valid value (such as "single") that can be present inside this attribute.

Multiple-Selection and Server-Side Scripts

When sending your multiple-selection menu to a server-side script via POST or GET, you'll notice that some server-side languages will not parse all the values selected (if more than one) and create an array. Instead, it will only give you the last option which is selected in the menu. To fix this, we can change the name of the selection menu to account for all options by putting them into an array, specifying [] at the end. This will work for most programming languages, including PHP.

<select multiple="multiple" name="myselect[]">...</select>

Pre-Selecting an Option

Pre-selection is handy for providing a default option that the average user would select or that the system recommends being used (such as a privacy setting). Any option can be selected by default. When used with a multiple-selection menu, as many options as desired can be selected by default. However, when used with a single-selection menu, the last option which is set as selected will appear as selected when rendered by the browser. If no option is set to be selected in a single-selection menu, the first option in the menu will be selected by default. Usually, this option is left as an empty value for easy detection by server-side scripts of whether or not a user selected anything.

The Syntax for Pre-Selection

<option value="some-text" selected="selected">Some Text</option>

Note: While most browsers will still render the menu correctly by only using selected, it is considered invalid syntax and you should always use the full selected="selected" format when pre-selecting an option. There is no other valid value (such as "none" or "unselected") that can be present inside this attribute.

Grouping Options Together

You can further organize your options into "categories" by using the <optgroup> element, which (as you'd imagine) groups the options within it under a certain title. This title appears in a slightly different style above the group of options it represents, and cannot be selected by the user (only an option can). Also, just because some options are within an option group doesn't mean all options have to be inside one. The title that is used is specified by its label attribute. For example:

<optgroup label="Apples">
    <option value="granny-smith">Granny Smith</option>
    <option value="red-delicious">Red Delicious</option>
</optgroup>
<option value="bananas">Bananas</option>

Browser Support

The <select> tag is supported by all browsers.

5157 questions
160
votes
10 answers

Is it possible to use JS to open an HTML select to show its option list?

Is it possible to use JavaScript to open an HTML select to show its option list?
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
145
votes
15 answers

Is it possible to style a select box?

I've got an HTML select box that I need to style. I'd prefer to use just CSS but if I have to I'll use jQuery to fill in the gaps. Can anyone recommend a good tutorial or plugin? I know, Google, but I've been searching for the last two hours and I'm…
dougoftheabaci
141
votes
12 answers

How to use Checkbox inside Select Option

The client has given me a design which has a Select Option menu containing a checkbox together with the item name as individual items in the list. Is there anyway possible to add a checkbox inside a Select Option menu? NB: Developer needs to add his…
Hero Tra
  • 1,443
  • 2
  • 10
  • 3
132
votes
9 answers

Default text which won't be shown in drop-down list

I have a select which initially shows Select language until the user selects a language. When the user opens the select, I don't want it to show a Select language option, because it's not an actual option. How can I achieve this?
Vitalii Ponomar
  • 10,686
  • 20
  • 60
  • 88
131
votes
18 answers

Can an Option in a Select tag carry multiple values?

I got a select tag with some options in a HTML form: (the data will be collected and processed using PHP) Testing: Is it possible…
user327712
  • 3,291
  • 4
  • 37
  • 45
130
votes
3 answers

Remove Select arrow on IE

I have select element, i want to remove the arrow, then i can add other icon.. i can do that for Firefox Safari and Chrome, but this didn't work on IE9. .styled-select select { border: 0 !important; /*Removes border*/ -webkit-appearance:…
Muath
  • 4,351
  • 12
  • 42
  • 69
124
votes
16 answers

Make multiple-select to adjust its height to fit options without scroll bar

Apparently this doesn't work: select[multiple]{ height: 100%; } it makes the select have 100% page height... auto doesn't work either, I still get the vertical scrollbar. Any other ideas?
Alex
  • 66,732
  • 177
  • 439
  • 641
120
votes
5 answers

How to get equal width of input and select fields

On the form, I have one select and two input fields. These elements are vertically aligned. Unfortunately, I can't get equal width of these elements. Here's my code: element with cross-browser compatibility? I know many…
Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852
106
votes
9 answers

Drop Down Menu/Text Field in one

I'm working on building new site, and I need a drop down menu to select the amount of something in my site. But at the same time I need this drop down list to accept text. So if the client wants to choose from the drop down list then he can, also if…
khalid jarrah
  • 1,561
  • 5
  • 14
  • 18
103
votes
28 answers

How to get all selected values of a multiple select box?

I have a