115

How implement subj?

when i write:

<form>
  <select>
     <option value="0">aaaa</option>
     <option value="1">bbbb</option>
  </select>
</form>

then default selected item is "aaaa"

when i write:

<form>
  <select>
     <option value=""></option>
     <option value="0">aaaa</option>
     <option value="1">bbbb</option>
  </select>
</form>

then default selected item is blank, but this blank item presents in drop down.

how i can implement SELECT tag with default blank value that hidden in dropdown list?

Nick Stavrogin
  • 1,201
  • 2
  • 8
  • 6
  • 1
    Possible duplicate of [default select option as blank](http://stackoverflow.com/questions/8605516/default-select-option-as-blank) – Blazemonger Oct 27 '15 at 20:39

8 Answers8

189

Just use disabled and/or hidden attributes:

<option selected disabled hidden style='display: none' value=''></option>
  • selected makes this option the default one.
  • disabled makes this option unclickable.
  • style='display: none' makes this option not displayed in older browsers. See: Can I Use documentation for hidden attribute.
  • hidden makes this option to don't be displayed in the drop-down list.
John Zabroski
  • 2,212
  • 2
  • 28
  • 54
Eduardo
  • 1,891
  • 1
  • 11
  • 2
68

You can by setting selectedIndex to -1 using .prop: http://jsfiddle.net/R9auG/.

For older jQuery versions use .attr instead of .prop: http://jsfiddle.net/R9auG/71/.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
36

Simply using

<option value="" selected disabled>Please select an option...</option>

will work anywhere without script and allow you to instruct the user at the same time.

mvreijn
  • 2,807
  • 28
  • 40
24
<select>
     <option value="" style="display:none;"></option>
     <option value="0">aaaa</option>
     <option value="1">bbbb</option>
  </select>
Andrey Ravkov
  • 1,268
  • 12
  • 21
  • 1
    ff/chrome - ok, opera/ie7-9 - not ok you can test that here (with or without js): http://jsfiddle.net/R9auG/145/ so I recommend js-approach by @pimvdb – tibalt Oct 04 '12 at 04:11
11

Here is a simple way to do it using plain JavaScript. This is the vanilla equivalent of the jQuery script posted by pimvdb. You can test it here.

<script type='text/javascript'>
  window.onload = function(){
    document.getElementById('id_here').selectedIndex = -1;
  }
</script>

.

<select id="id_here">
  <option>aaaa</option>
  <option>bbbb</option>
</select>

Make sure the "id_here" matches in the form and in the JavaScript.

Mingwei Samuel
  • 2,917
  • 1
  • 30
  • 40
3

For purely html @isherwood has a great solution. For jQuery, give your select drop down an ID then select it with jQuery:

<form>
  <select id="myDropDown">
    <option value="0">aaaa</option>
    <option value="1">bbbb</option>
  </select>
</form> 

Then use this jQuery to clear the drop down on page load:

$(document).ready(function() {
    $('#myDropDown').val(''); 
});

Or put it inside a function by itself:

$('#myDropDown').val(''); 

accomplishes what you're looking for and it is easy to put this in functions that may get called on your page if you need to blank out the drop down without reloading the page.

Jason Slobotski
  • 1,386
  • 14
  • 18
3

You can't. They simply do not work that way. A drop down menu must have one of its options selected at all times.

You could (although I don't recommend it) watch for a change event and then use JS to delete the first option if it is blank.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 3
    Why don't you recommend this behavior? – Josh Noe Jun 01 '12 at 15:17
  • There are plenty of cases where you want the user to actively pick something from a list. I don't think JS is the most optimal way of doing it (although it seems to be the only way), but i really think there *should* be a way. – qwerty May 14 '13 at 17:08
0

You can try this snippet

$("#your-id")[0].selectedIndex = -1

It worked for me.

dgellow
  • 692
  • 1
  • 11
  • 18
Shenbag
  • 9
  • 1