Set values in jquery multiselect dropdown
Asked Answered
A

3

11

I want to set the multiple ITEMS in jQuery multiselect dropdown depending on the VALUE of that item, which I'm getting from database, separated by ",".

I'm saving this fetched data in hidden field.

Ex.

Hidden field: hdnLots = 64,65 , Items for lot No.: 64 = Lot 1, 65 = Lot2

So when I get the hdnLot=65, then in jQuery Multiselect dropdown only Lot2 needed to be selected. And same for 64,65. i.e. When multiple values are selected than all those values needed to seleted.

I've tried: JQuery multiselect - Set a value as selected in the multiselect dropdown

Code I've wrote for this is:

if ($("#<%= btnUpdateProject.ClientId %>").css('display') == "inline-block") 
{
   debugger;
   var dataarray = document.getElementById('<%= hdnLots.ClientId %>').value.split(",");
   $("#<%= ddlNoOfLots.clientid %>").val(dataarray);
}
Acidfast answered 4/1, 2014 at 4:52 Comment(15)
Add ` $("#<%= ddlNoOfLots.clientid %>").refresh();`Makeweight
add $("#<%= ddlNoOfLots.clientid %>").multiselect("refresh");Correlate
$("#<%= ddlNoOfLots.clientid %>").multiselect("refresh");Gurias
@Makeweight : Its showing error.Acidfast
@user3085495 what error?Gurias
@Correlate : I've tried it, but its changing size(height,width) of multiselect and neither its working.Acidfast
@Makeweight : Uncaught type error: Object [object object] has no method refresh.Acidfast
Sorry but $("#<%= ddlNoOfLots.clientid %>").multiselect('refresh'); is the right syntax. Also have you checked what is the value of the dataarray varMakeweight
Can you reproduce the problem on a demo?Correlate
@Makeweight : It's showing only the single data in dataarray. i.e. dataarray = 65 only, even if hdnLots=64,65.Acidfast
@Dharmang: I'll try for that, but I don't think so I can do that because I'm new to this.Acidfast
You need use third party service like jsfiddle to demonstrate your code in js and html language, if you don't know already.Correlate
Try alerting document.getElementById('<%= hdnLots.ClientId %>').value and see what it holdsMakeweight
@Dharmang: Somehow I managed to reach upto refresh method but its still showing this error: Uncaught Error: Cannot call methods on multiselect prior to initialization, attempt to call method 'refresh' in console.Acidfast
This error means that your multiselect instance is not initialized. first initialize it by calling $("#<%= ddlNoOfLots.clientid %>").multiselect(); provide any options if necessary.Correlate
C
21

I hope this will help you:

Demo

$(document).ready(function() {
$("select").multiselect({
   selectedText: "# of # selected"
});
var hidValue = $("#hidSelectedOptions").val();
alert(hidValue);
var selectedOptions = hidValue.split(",");
for(var i in selectedOptions) {
    var optionVal = selectedOptions[i];
    $("select").find("option[value="+optionVal+"]").prop("selected", "selected");
}
$("select").multiselect('reload');
});

EDIT

refresh has been removed from latest jQuery-MultiSelect. Using reload will solve the question now.

Correlate answered 4/1, 2014 at 5:46 Comment(0)
R
7

The one thing you would need to ensure is that the values in the array are strings:

<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/

Rugby answered 14/6, 2016 at 2:4 Comment(1)
I'm glad I could help mate!Rugby
C
5
var selectedOptions = hidValue.split(",");
typeof (selectedOptions != 'undefined' && $("#hidSelectedOptions").multiselect('select', selectedOptions));
Chibcha answered 27/9, 2017 at 23:18 Comment(1)
.multiselect('select', selectedOptions)); this part works great for me.Jimmiejimmy

© 2022 - 2024 — McMap. All rights reserved.