hi all i am binding my dropdown with Jquery-Select2. Its working fine but now i need to Bind my Multi-Value selectBox by using Jquery-Select2.
M
Using select2 library there are 2 ways to set the value 1. when single value is present you can pass it as string
$("#elementid").select2("val","valueTobeset")
2. when you are using select2 with multiselect option you can pass an array of the values to select2 and all those values will be set
var arrayOfValues = ["a","c"]
$("#elementid").select2("val",arrayOfValues)
remember that you can set single value on multiselect by passing an array also like this
var arrayOfValues = ["a"]
$("#elementid").select2("val",arrayOfValues)
This is with reference to the original question
$('select').val(['a','c']);
$('select').trigger('change');
This doesn't work. only one value is ever pre-selected even though both options are available in the list only the first is shown
('#searchproject').select2('val', ['New Co-location','Expansion']);
Use multiselect function as below.
$('#drp_Books_Ill_Illustrations').multiSelect('select', 'value');
So I take it you want 2 options default selected, and then get the value of it? If so:
http://jsfiddle.net/NdQbw/1/
<div class="divright">
<select id="drp_Books_Ill_Illustrations" class="leaderMultiSelctdropdown Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple="">
<option value=" ">No illustrations</option>
<option value="a" selected>Illustrations</option>
<option value="b">Maps</option>
<option value="c" selected>selectedPortraits</option>
</select>
</div>
And to get value:
alert($(".leaderMultiSelctdropdown").val());
To set the value:
$(".leaderMultiSelctdropdown").val(["a", "c"]);
You can also use an array to set the values:
var selectedValues = new Array();
selectedValues[0] = "a";
selectedValues[1] = "c";
$(".Books_Illustrations").val(selectedValues);
http://jsfiddle.net/NdQbw/4/
Just look at this one it will helps.
var valoresArea=VALUES // it has the multiple values to set, separated by comma
var arrayArea = valoresArea.split(',');
$('#area').val(arrayArea);
the URL is- link