Search code examples
twitter-bootstrap-3bootstrap-select

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>

Solution

  • 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