Method 1 - Upgrade Bootstrap-Select Version 1.7
In the release notes for Version 1.7.0 :
# 888 , # 738 : Show "title" when using not multiple selection An empty parameter is added to the selection, which is then selected by default. This allows you to display the title when the selection is first downloaded and no.
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.0/css/bootstrap-select.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.0/js/bootstrap-select.js"></script>
Demo with stack fragments :
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.0/css/bootstrap-select.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.0/js/bootstrap-select.js"></script> <select class="selectpicker" > <option data-hidden="true">Pick One</option> <option>Mustard</option> <option>Ketchup</option> <option>Relish</option> </select>
Method 2 - Hide an empty option using CSS
You can hide the first <li> element in a drop-down menu like this (although you probably want to base it on a class so that this does not happen by default).
.bootstrap-select ul.dropdown-menu li:first-child { display: none; }
Demo with stack fragments :
.bootstrap-select ul.dropdown-menu li:first-child { display: none; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.0/css/bootstrap-select.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.0/js/bootstrap-select.js"></script> <select class="selectpicker" title="Pick One" > <option></option> <option>Mustard</option> <option>Ketchup</option> <option>Relish</option> </select>
Method 3 - Hide an empty option with data attributes
As suggested by @Antiga, you can hide the first option with data-hidden="true" as follows:
<select class="selectpicker"> <option data-hidden="true">Pick One</option> <option>Mustard</option> <option>Ketchup</option> <option>Relish</option> </select>
Demo with stack fragments :
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.0/css/bootstrap-select.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.0/js/bootstrap-select.js"></script> <select class="selectpicker" > <option data-hidden="true">Pick One</option> <option>Mustard</option> <option>Ketchup</option> <option>Relish</option> </select>
Kylemit
source share