I have a drop down where the user selects a language:
<select>
<option>English</option>
<option>Spanish</option>
</select>
This question is related to
html
html-select
Building on Oded's answer, you could also set the default option but not make it a selectable option if it's just dummy text. For example you could do:
<option selected="selected" disabled="disabled">Select a language</option>
This would show "Select a language" before the user clicks the select box but the user wouldn't be able to select it because of the disabled attribute.
Although this question has an accepted answer but I think this is a much cleaner way to achieve the desired output
<select required>
<option value="">Select</option>
<option>English</option>
<option>Spanish</option>
</select>
The required attribute in makes it mandatory to select an option from the list.
value="" inside the option tag combined with the required attribute in select tag makes selection of 'Select' option not permissible, thus achieving the required output
Just make option#1 Select Language:
Put your prompt in the 1st option
and disable it:
<selection>
<option disabled selected>”Select a language”</option>
<option>English</option>
<option>Spanish</option>
</selection>
The first option will automatically be the selected default (what you see first when you look at the drop-down) but adding the selected
attribute is more clear and actually needed when the first field is a disabled field.
The disabled
attribute will make the option be un-selectable/grayed out.
Other answers suggest setting disabled=“disabled”
but that’s only necessary if you need to parse as XHTML, which is basically a more strict version of HTML. disabled
on it’s on is enough for standard HTML.
If you want to make the selection “required” (without accepting the “Select a language” option as an accepted answer):
Add the required
attribute to selection
and set the first option
’s value
to the empty string ””
.
<selection required>
<option disabled value=“”>Select a language</option>
<option>English</option>
<option>Spanish</option>
</selection>
<option value="" selected disabled hidden>Default Text</option>
Leaving the disabled flag in prevents them from not selecting an option and the hidden flag will remove it from the list. In my case I was using it with an enum list as well and the concept holds the same
<select asp-for="Property" asp-items="Html.GetEnumSelectList<PropertyEnum>()">
<option value="" selected disabled hidden>Select Property Enum</option>
<option value=""></option>
</select>
.selectmenu{_x000D_
_x000D_
-webkit-appearance: none; /*Removes default chrome and safari style*/_x000D_
-moz-appearance: none; /* Removes Default Firefox style*/_x000D_
background: #0088cc ;_x000D_
width: 200px; /*Width of select dropdown to give space for arrow image*/_x000D_
text-indent: 0.01px; /* Removes default arrow from firefox*/_x000D_
text-overflow: ""; /*Removes default arrow from firefox*/ /*My custom style for fonts*/_x000D_
color: #FFF;_x000D_
border-radius: 2px;_x000D_
padding: 5px;_x000D_
border:0 !important;_x000D_
box-shadow: inset 0 0 5px rgba(000,000,000, 0.5);_x000D_
}_x000D_
.hideoption { display:none; visibility:hidden; height:0; font-size:0; }
_x000D_
Try this html_x000D_
_x000D_
<select class="selectmenu">_x000D_
<option selected disabled class="hideoption">Select language</option>_x000D_
<option>Option 1</option>_x000D_
<option>Option 2</option>_x000D_
<option>Option 3</option>_x000D_
<option>Option 4</option>_x000D_
<option>Option 5</option>_x000D_
</select>
_x000D_
Source: Stackoverflow.com