[html] default select option as blank

I have a very weird requirement, wherein I am required to have no option selected by default in drop down menu in HTML. However,

I cannot use this,

_x000D_
_x000D_
<select>_x000D_
  <option></option>_x000D_
  <option>Option 1</option>_x000D_
  <option>Option 2</option>_x000D_
  <option>Option 3</option>_x000D_
</select>
_x000D_
_x000D_
_x000D_

Because, for this I will have to do validation to handle the first option. Can anyone help me in achieving this target without actually including the first option as part of the select tag?

This question is related to html

The answer is


just use "..option hidden selected.." as default option


You could use Javascript to achieve this. Try the following code:

HTML

<select id="myDropdown">      
    <option>Option 1</option>     
    <option>Option 2</option>     
    <option>Option 3</option>     
</select> 

JS

document.getElementById("myDropdown").selectedIndex = -1;

or JQuery

$("#myDropdown").prop("selectedIndex", -1);

This should help :

https://www.w3schools.com/tags/att_select_required.asp

_x000D_
_x000D_
 <form>_x000D_
 <select required>_x000D_
  <option value="">None</option>_x000D_
  <option value="volvo">Volvo</option>_x000D_
  <option value="saab">Saab</option>_x000D_
  <option value="mercedes">Mercedes</option>_x000D_
  <option value="audi">Audi</option>_x000D_
</select>_x000D_
<button type="submit">Submit</button>_x000D_
</form>
_x000D_
_x000D_
_x000D_


If you are using Angular (2+), (or any other framework), you could add some logic. The logic would be: only display an empty option if the user did not select any other yet. So after the user selected an option, the empty option disappears.

For Angular (9) this would look something like this:

<select>
    <option *ngIf="(hasOptionSelected$ | async) === false"></option>
    <option *ngFor="let option of (options$ | async)[value]="option.id">{{ option.title }}</option>
</select>

Solution that works by only using CSS:

A: Inline CSS

_x000D_
_x000D_
<select>_x000D_
    <option style="display:none;"></option>_x000D_
    <option>Option 1</option>_x000D_
    <option>Option 2</option>_x000D_
    <option>Option 3</option>_x000D_
</select>
_x000D_
_x000D_
_x000D_

B: CSS Style Sheet

If you have a CSS file at hand, you can target the first option using:

_x000D_
_x000D_
select.first-opt-hidden option:first-of-type {_x000D_
  display:none;_x000D_
}
_x000D_
<select class="first-opt-hidden">_x000D_
    <option></option>_x000D_
    <option>Option 1</option>_x000D_
    <option>Option 2</option>_x000D_
    <option>Option 3</option>_x000D_
</select>
_x000D_
_x000D_
_x000D_


Today (2015-02-25)

This is valid HTML5 and sends a blank (not a space) to the server:

<option label=" "></option>

Verified validity on http://validator.w3.org/check

Verified behavior with Win7(IE11 IE10 IE9 IE8 FF35 Safari5.1) Ubuntu14.10(Chrome40, FF35) OSX_Yosemite(Safari8, Chrome40) Android(Samsung-Galaxy-S5)

The following also passes validation today, but passes some sort of space character too the server from most browsers (probably not desirable) and a blank on others (Chrome40/Linux passes a blank):

<option>&#160;</option>

Previously (2013-08-02)

According to my notes, the non-breaking-space entity inside the option tags shown above produced the following error in 2013:

Error: W3C Markup Validaton Service (Public): The first child option element of a select element with a required attribute and without a multiple attribute, and whose size is 1, must have either an empty value attribute, or must have no text content.

At that time, a regular space was valid XHTML4 and sent a blank (not a space) to the server from every browser:

<option> </option>

Future

It would make my heart glad if the spec was updated to explicitly allow a blank option. Preferably using the briefest syntax. Either of the following would be great:

<option />
<option></option>

Test File

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Test</title>
</head>
<body>
  <form action="index.html" method="post">
    <select name="sel">
      <option label=" "></option>
    </select>
  </form>
</body>
</html>

If you don't need any empty option at first, try this first line:

    <option style="display:none"></option>

For those who are using <select multiple> (combobox; no dropdown), this worked for me:

_x000D_
_x000D_
<select size=1 disabled multiple>
  <option hidden selected></option>
  <option>My Option</option>
</select>
_x000D_
_x000D_
_x000D_


Try this:

<h2>Favorite color</h2>
<select name="color">
<option value=""></option>
<option>Pink</option>
<option>Red</option>
<option>Blue</option>
</select>

The first option in the drop down would be blank.


<select required>
  <option value="" disabled selected>None</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

You can avoid custom validation in this case.


In order to show please select a value in drop down and hide it after some value is selected . please use the below code.

it will also support required validation.

_x000D_
_x000D_
<select class="form-control" required>_x000D_
<option disabled selected value style="display:none;">--Please select a value</option>_x000D_
              <option >Data 1</option>_x000D_
              <option >Data 2</option>_x000D_
              <option >Data 3</option>_x000D_
</select>
_x000D_
_x000D_
_x000D_


_x000D_
_x000D_
<td><b>Field Label:</b><br>
    <select style='align:left; width:100%;' id='some_id' name='some_name'>
    <option hidden selected>Select one...</option>
    <option value='Value1'>OptLabel1</option>
    <option value='Value2'>OptLabel2</option>
    <option value='Value3'>OptLabel3</option></select>
</td>
_x000D_
_x000D_
_x000D_

Just put "hidden" on option you want to hide on dropdown list.


I found it really interesting because I just experienced the same thing not so long time ago. However, I came across to an example on the Internet about the solution regarding this.

Without any further ado, see the code fragment below:

<select>
 <option value data-isdefault="true">--Choose one Option--</option>
 <option>Option 1</option>
 <option>Option 2</option>
 <option>Option 3</option>
</select>

With that, it will stay un-submittable but selectable, anytime. More convenience for User Interface and great for User Experience.

Well that's all, I hope it helps. Cheers!


Try this:

<select>
    <option value="">&nbsp;
    <option>Option 1
    <option>Option 2
    <option>Option 3
</select>

Validates in HTML5. Works with required attribute in select element. Can be re-selected. Works in Google Chrome 45, Internet Explorer 11, Edge, Firefox 41.


Just a small remark:

some Safari browsers do not seem to respect neither the "hidden" attribute nor the style setting "display:none" (tested with Safari 12.1 under MacOS 10.12.6). Without an explicit placeholder text, these browsers simply show an empty first line in the list of options. It may therefore be useful to always provide some explanatory text for this "dummy" entry:

<option hidden disabled selected value>(select an option)</option>

Thanks to the "disabled" attribute, it won't be actively selected anyway.


I'm using Laravel 5 framework and @Gambi `s answer worked for me as well but with some changes for my project.

I have the option values in a database table and I use them with a foreach statement. But before the statement I have added an option with @Gambit suggested settings and it worked.

Here my exemple:

@isset($keys)
  <select>
    <option  disabled selected value></option>
    @foreach($keys as $key)
      <option>{{$key->value)</option>
    @endforeach
  </select>
@endisset 

I hope this helps someone as well. Keep up the good work!


There is no HTML solution. By the HTML 4.01 spec, browser behavior is undefined if none of the option elements has the selected attribute, and what browsers do in practice is that they make the first option pre-selected.

As a workaround, you could replace the select element by a set of input type=radio elements (with the same name attribute). This creates a control of the same kind though with different appearance and user interface. If none of the input type=radio elements has the checked attribute, none of them is initially selected in most modern browsers.


I understand what you are trying to do.The best and the most successful way is :

<select name='department' required>
   <option value="">None</option>
   <option value="Teaching">Teaching department</option>
   <option value="nonTeaching">Non-teaching department</option> 
</select>