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
1
vote
1 answer

I want to create dynamic select option list using javascript or jQuery

I have 3 arrays: one for the value, and 2nd one for the option name, and the third one for the price. I want to create a dynamic option list using value and option name (value is channel id and channel name) and, on selection of the channel name,…
ssatish4u
  • 131
  • 2
  • 9
1
vote
2 answers

jquery selective display of form inputs based on selected dropdown option

$( document ).ready(function() { return $("body").on("change",".creator_name_type", function(event){ if (event.target.value == 'Personal') { $(this).siblings(".organization_name").hide(); …
brg
  • 3,915
  • 8
  • 37
  • 66
1
vote
2 answers

PHP How to make a dropdown with database data

So I have this database and it has column called selectedDates. In that column there is a row with the values 2018-08-22 2018-08-15 2018-08-20 Keep in mind this is 3 sets of dates, but in one row. How do I make a select tag/dropdown with these…
user10183717
1
vote
2 answers

get option values in array using map jquery

$('#btnadd').click(addBulletinBoard); function addBulletinBoard() { var show = $('#show').val(); var show1; console.log("before " + $('#show').val()) if (jQuery.inArray("*", show) !== -1) { show1 = $("select#show…
Manlapig
  • 33
  • 9
1
vote
2 answers

Ionic 3 - ion-select not able to see values of select option

I am working on select option menus. I am not able to see values on drop down selection. I don't know what am I doing wrong. Please go through below code once 1. HTML
Flutterian
  • 1,761
  • 1
  • 21
  • 47
1
vote
3 answers

Running javascript function when option selected

I want to execute a function depending on which option in a dropdown menu is selected. For now I just wrote document.write() function to test if anything is happening. However, nothing is printing, and the code is not working as I wanted.…
Melanie
  • 313
  • 2
  • 6
  • 17
1
vote
1 answer

CSS/Form dropdown arrow positioning

So I'm trying to code an "add to cart" area, where you can select between a few product options and then click a button and add them to the cart.
Huma Ali
  • 1,759
  • 7
  • 40
  • 66
1
vote
1 answer

Image or font awesome as option in select - Reactjs

I would like to have an input select with a choice of image as an option (would define a custom icon). I can put an image or an icon with Font awesome. I tried putting or replace …
Cohchi
  • 543
  • 3
  • 9
  • 19
1
vote
5 answers

.change() adds two values at the same time

Good day, In my code, I need to use a drop-down list that has two values, say 1 and 2. When I click the #action-link link I append the corresponding text of the value into a textarea (#main-reply). I use Jquery .change() method to determine when…
1
vote
1 answer

Set selected items in asp.core razor pages

I'm using ASP Core Razor pages. This my Edit.cshtml.cs: [BindProperty] public List CCusers { get; set; } and here I fill CCusers with data: CCusers =new…
salahaddin
  • 23
  • 1
  • 1
  • 5
1
vote
1 answer

options attribute of select element does not work in vue.js 2.0

I read in Vue.js 0.12 Guide that you can dynamically produce option tags inside a select tag with options attribute like below: But it does not work in Vue.js 2.0 and I have to use v-for…
Omid Sadeghi
  • 675
  • 7
  • 16
1
vote
1 answer

grouped_collection_select, Devise and Rails 5.0 custom registration issue

I'm having having some issues with custom Devise registration in Rails 5. I have 3 models with the following AR associations User (Devise) belongs_to :chapter Chapter belongs_to :country has_many :users Country has_many :chapters I created a…