Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Bootstrap Multiselect plugin - Set a value as selected in the multiselect dropdown

I am having a multiselect dropdown using the Boostrap Multiselect plugin (http://davidstutz.de/bootstrap-multiselect/) as below

<select id="data" name="data" class="data" multiple="multiple">   <option value="100">foo</option>   <option value="101">bar</option>   <option value="102">bat</option>   <option value="103">baz</option> </select> 

On load of page, i will get an array of value like [101,102]. I should iterate through the array and make the values selected(check boxes corresponding to the ids should be checked). Please help.

like image 590
Prince Avatar asked Aug 06 '11 10:08

Prince


People also ask

How do I get the selected value of multiselect dropdown in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.


2 Answers

//Do it simple  var data="1,2,3,4";  //Make an array  var dataarray=data.split(",");  // Set the value  $("#multiselectbox").val(dataarray);  // Then refresh  $("#multiselectbox").multiselect("refresh"); 
like image 123
Tapon Sayeem Avatar answered Sep 20 '22 13:09

Tapon Sayeem


Thank you all for your answers.

Taking all of your answers as a guideline, I resolved the problem with the below code:

var valArr = [101,102]; i = 0, size = valArr.length; for(i; i < size; i++){   $("#data").multiselect("widget").find(":checkbox[value='"+valArr[i]+"']").attr("checked","checked");   $("#data option[value='" + valArr[i] + "']").attr("selected", 1);   $("#data").multiselect("refresh"); } 

Thanks once again for all your support.

like image 37
Prince Avatar answered Sep 21 '22 13:09

Prince