Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Select - Set selected value by text

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>
like image 221
jagamot Avatar asked Dec 14 '22 08:12

jagamot


1 Answers

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

like image 67
Sathish Avatar answered Dec 22 '22 01:12

Sathish