Blank HTML SELECT without blank item in dropdown list

HtmlSelectDrop Down-Menu

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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.

Solution 2 - Html

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/.

Solution 3 - Html

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.

Solution 4 - Html

<select>
     <option value="" style="display:none;"></option>
     <option value="0">aaaa</option>
     <option value="1">bbbb</option>
  </select>

Solution 5 - Html

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.

Solution 6 - Html

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.

Solution 7 - Html

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.

Solution 8 - Html

You can try this snippet

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

It worked for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNick StavroginView Question on Stackoverflow
Solution 1 - HtmlEduardoView Answer on Stackoverflow
Solution 2 - HtmlpimvdbView Answer on Stackoverflow
Solution 3 - HtmlmvreijnView Answer on Stackoverflow
Solution 4 - HtmlAndrey RavkovView Answer on Stackoverflow
Solution 5 - HtmlMingwei SamuelView Answer on Stackoverflow
Solution 6 - HtmlQuentinView Answer on Stackoverflow
Solution 7 - HtmlJason SlobotskiView Answer on Stackoverflow
Solution 8 - HtmlShenbagView Answer on Stackoverflow