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

JqueryBootstrap MultiselectJquery Ui-Multiselect

Jquery Problem Overview


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.

Jquery Solutions


Solution 1 - Jquery

//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");

Solution 2 - Jquery

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.

Solution 3 - Jquery

It's supposed to be as simple as this:

<select id='multipleSelect' multiple='multiple'>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>
<script type='text/javascript'>
    $('#multipleSelect').val(['1', '2']);
</script>

Check my Fiddle: https://jsfiddle.net/luthrayatin/jaLygLzo/

Solution 4 - Jquery

var valArr = [101,102], // array of option values
    i = 0, size = valArr.length, // index and array size declared here to avoid overhead
    $options = $('#data option'); // options cached here to avoid overhead of fetching inside loop

// run the loop only for the given values
for(i; i < size; i++){
    // filter the options with the specific value and select them
    $options.filter('[value="'+valArr[i]+'"]').prop('selected', true);
}

Here is a demo

Solution 5 - Jquery

work fine for me ,change ids according to you requirement.

$("#FormId select#status_id_new").val('');
 $("#FormId select#status_id_new").multiselect("refresh");

Solution 6 - Jquery

that code should help:

var selected=[101,103];
var obj=$('#data');
for (var i in selected) {
    var val=selected[i];
    console.log(val);
   obj.find('option:[value='+val+']').attr('selected',1);
}

Solution 7 - Jquery

from a php array into the multiselect...

$PHP_array=array();           // create array anyway you need to in PHP
array_push($PHP_array,20);    // Single ID values
array_push($PHP_array,22);

$javascript_str = json_encode($PHP_array);   // JSON ENCODE        

// then in the JS script

$("#multiselectbox").val(<?php echo $javascript_str; ?>);

// Then refresh

$("#multiselectbox").multiselect("refresh");

Solution 8 - Jquery

 var valArr = ["1","2","3"];

/* Below Code Matches current object's (i.e. option) value with the array values */
 /* Returns -1 if match not found */
 $("select").multiselect("widget").find(":checkbox").each(function(){
	if(jQuery.inArray(this.value, valArr) !=-1)
			this.click();
	
   });

This selects the options whose value matches with values in array.

Solution 9 - Jquery

The official site has mentioned (in "Manually check or uncheck a checkbox?" section) just to use click()

e.g.

$("#ddl_singleSelect")
	.multiselect("widget")
		.find(":radio[value='']").click();	// change displayed selectedText
$("#ddl_singleSelect option[value='']")
	.prop("selected", true)				// change val()
	.trigger("change");					// fire change event


$("#ddl_multiselect").multiselect("uncheckAll");	// init by de-selecting all
$("#ddl_multiselect")
	.multiselect("widget")
		.find(	   ":checkbox[value='1']"
				+ ",:checkbox[value='2']"
				+ ",:checkbox[value='3']"
				+ ",:checkbox[value='4']"
			).click();	// change displayed selecetdText
$("#ddl_multiselect")
	.find(	   "option[value='1']"
			+ ",option[value='2']"
			+ ",option[value='3']"
			+ ",option[value='4']"
		).prop("selected", true);	// change val()
$("#ddl_multiselect").trigger("change");	// fire change event

OR by HTML

<select name="example-presets" multiple="multiple" size="5">
	<option value="option1" selected="selected">Option 1</option>
	<option value="option2">Option 2</option>
	<option value="option3" selected="selected">Option 3</option>
	<option value="option4" selected="selected">Option 4</option>
	<option value="option5" disabled="disabled">Option 5</option>
	<option value="option6" disabled="disabled">Option 6</option>
	<option value="option7">Option 7</option>
	<option value="option8">Option 8</option>
	<option value="option9">Option 9</option>
</select>
....
<script type="text/javascript">$("select").multiselect();</script>

Solution 10 - Jquery

I got a better solution from jquery-multiselect documentation.

var data = [101,102];

$("#data").multiSelect('deselect_all');

$("#data").multiSelect("select",data);

Solution 11 - Jquery

$("#dropDownId").val(["130860","130858"]);
$("#dropDownId").selectmenu('refresh');

Solution 12 - Jquery

this is my code sample. I have comma separated numbers for multi select drop down and want to select options multiple options from comma separated values.

var selected_tags_arr = new Array();
var selected_tags = 1,2,4;
selected_tags_arr = selected_tags.split(",");
$('#contact_tags option').each(function (){
	var option_val = this.value;
	for (var i in selected_tags_arr) {
		if(selected_tags_arr[i] == option_val){
			$("#contact_tags option[value='" + this.value + "']").attr("selected", 1);
		}
	}
});

Solution 13 - Jquery

This is what worked from me using an array as input. First uncheck all options, then click on the chosen options using jquery

var dimensions = ["Dates","Campaigns","Placements"];

$(".input-dimensions").multiselect("uncheckAll");

for( var dim in dimensions) {
  $(".input-dimensions").multiselect("widget")
    .find(":checkbox[value='" + dimensions[dim] + "']").click();
}

Solution 14 - Jquery

I ended up having to make a slight change using the click event on the input element by also setting the checked prop after firing the click event.

$(y.Ctrl).multiselect("widget").find(":checkbox").each(function () {
    if ($.inArray(this.value, uniqueVals) != -1) {
        $(this).click();
        $(this).prop('checked', true);
    }
});

Solution 15 - Jquery

I hope these functions will help you.

$('#your-select').multiSelect('select', String|Array);

$('#your-select').multiSelect('deselect', String|Array);

$('#your-select').multiSelect('select_all');

$('#your-select').multiSelect('deselect_all');

$('#your-select').multiSelect('refresh');

Reference by this site http://loudev.com/" target="_blank"> Reference

Solution 16 - Jquery

Selects an option by its value. Works also using an array of values. Find complete details http://davidstutz.github.io/bootstrap-multiselect/#methods

$('#example-select').multiselect('select', ['1', '2', '4']); 

Solution 17 - Jquery

You can try this:

$( "#data" ).val([ "100", "101" ]);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrinceView Question on Stackoverflow
Solution 1 - JqueryTapon SayeemView Answer on Stackoverflow
Solution 2 - JqueryPrinceView Answer on Stackoverflow
Solution 3 - JqueryYatinView Answer on Stackoverflow
Solution 4 - JqueryShefView Answer on Stackoverflow
Solution 5 - JqueryBasant RulesView Answer on Stackoverflow
Solution 6 - Jqueryzb'View Answer on Stackoverflow
Solution 7 - JqueryPodTech.ioView Answer on Stackoverflow
Solution 8 - JqueryAnubhav TrivediView Answer on Stackoverflow
Solution 9 - JqueryvientohoView Answer on Stackoverflow
Solution 10 - JquerySreeView Answer on Stackoverflow
Solution 11 - Jqueryamit kateView Answer on Stackoverflow
Solution 12 - Jquerymahesh kajaleView Answer on Stackoverflow
Solution 13 - JqueryLaurent JacquotView Answer on Stackoverflow
Solution 14 - JqueryAdamView Answer on Stackoverflow
Solution 15 - JqueryLetsCMS Pvt LtdView Answer on Stackoverflow
Solution 16 - JqueryYawar AliView Answer on Stackoverflow
Solution 17 - JqueryaatishView Answer on Stackoverflow