Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select dynamically in Bootstrap-select with multiple values

How can I select dynamically in Bootstrap-select with multiple values, if my values are 1,3,4, using jQuery?

Here is my select:

 <select  id="myselect" name="myselect[]" multiple>
        <option value=""></option>
        <option value="1">red</option>
        <option value="2">orange</option>
        <option value="3">green</option>
        <option value="4">blue</option>
</select>
like image 209
jemz Avatar asked Sep 04 '15 01:09

jemz


3 Answers

Use Bootstrap-Select's val method:

$('#myselect').selectpicker('val', [1,3,4]);

http://jsfiddle.net/a4bxnwws/

See Bootstrap-Select's documentation.

like image 116
David Stosik Avatar answered Oct 19 '22 10:10

David Stosik


You can set the value for the select element using two methods.

For the first one, you can use the method that the bootstrap-select plugin provided: selectpicker(just like the above answer);

The second one is using jquery's method - trigger. Such as:

$('#myselect').val([1,3,4]).trigger('change');
like image 2
Joy Avatar answered Oct 19 '22 11:10

Joy


If you use selectpicker class then

<select class="selectpicker" id="myselect" name="myselect[]" multiple>
    <option value=""></option>
    <option value="1">red</option>
    <option value="2">orange</option>
    <option value="3">green</option>
    <option value="4">blue</option>
</select>

And Your jquery code will be like below:

var select_items = ["1","3","4"];
$('#myselect').selectpicker('val', select_items);
like image 2
RokiDGupta Avatar answered Oct 19 '22 12:10

RokiDGupta