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.
As of now this way is supported to update multiple values:
$("#UserRole").val(str_array).trigger("chosen:updated");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With