Bootstrap Select - Set selected value by text

jagamot picture jagamot · Feb 8, 2016 · Viewed 18.8k times · Source

How can I change the drop down if I only know the text and not the value?

Here is my select -

 <select id="app_id" class="selectpicker">
       <option value="">--Select--</option>
       <option value="1">Application 1</option>
       <option value="2">Application 2</option>
 </select>

Answer

Sathish picture Sathish · Feb 8, 2016

You can use jquery filter() and prop() like below

jQuery("#app_id option").filter(function(){
    return $.trim($(this).text()) ==  'Application 2'
}).prop('selected', true);

DEMO

Using BOOTSTRAP SELECT,

jQuery("#app_id option").filter(function(){
    return $.trim($(this).text()) ==  'Application 2'
}).prop('selected', true);
$('#app_id').selectpicker('refresh');

DEMO