Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery chosen set selected values from javascript

I am using jquery chosen and I want to set selected values from javascript

Javascript/Jquery

$(document).ready(function() {
  var selectedUserRole = document.getElementById('SelectedUserRole').value;
  var str_array = selectedUserRole.split(',');

  for (var i = 0; i < str_array.length; i++) {
    // Trim the excess whitespace.
    str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
    // Add additional code here, such as:

    $("UserRole").prop('selectedValue', str_array[i]); // Here not working
  }
});

Html:

 @Html.DropDownList("UserRole", null, new { id = "UserRole", @class = "chosen-select", @multiple = "multiple", @placeholder = "Lütfen Rol Seçiniz", @style = "width: 250px;" })

str_array has values below

[5, 1, 7]

I want to set 5 value selected, 1 value selected 7 value selected

When I tried below

$("UserRole").prop('selectedValue', str_array[i]);

it is not working for me.

like image 724
Beyto Avatar asked Feb 24 '15 08:02

Beyto


1 Answers

As of now this way is supported to update multiple values:

$("#UserRole").val(str_array).trigger("chosen:updated");
like image 62
Claus Lens Avatar answered Oct 08 '22 01:10

Claus Lens