How to check if a specific value in Kendo UI Multiselect was picked?

4.7k Views Asked by At

Html:

<select id="days" multiple="multiple" data-placeholder="Select days...">
    <option value="Monday">Monday</option>
    <option value="Tuesday">Tuesday</option>
    <option value="Wednesday">Wednesday</option>
    <option value="Thursday">Thursday</option>          
    <option value="Friday">Friday</option>          
    <option value="Saturday">Saturday</option>          
    <option value="Sunday">Sunday</option>          
</select>

Javascript:

$("#days").kendoMultiSelect({
    autoClose: false
});

var days = $("#days").data("kendoMultiSelect");

How do I find out if a specific item was selected? For instance, how do I know if user selected Friday?

Note that I can select multiple items here.

3

There are 3 best solutions below

3
On BEST ANSWER

There's no API method to check whether a specific item is selected, but you can easily create one, e.g. like this:

kendo.ui.MultiSelect.fn.isSelected = function (key) {
    var selectedItems = this.value();
    return selectedItems.indexOf(key) > -1;
};      

which you can then use like this:

$("#days").kendoMultiSelect({
    autoClose: false
});

var days = $("#days").data("kendoMultiSelect");

$("#button").click(function() {
    console.log("is it friday?", days.isSelected("Friday"));
});
1
On

You could give the switch a try.

var day = document.getElementById("days").value;
switch(day) {
    case "Monday":
        yourFunction();
        break;
    case "Tuesday":
        anotherFunction();
        break;

    ... etc.
}

Alright, Try something like this instead? Untested, mind you.

var days = [];
var options = document.querySelectorAll("#days option");
var i = 0, length = options.length;
for (i; i < length; i++) {
    if (options[i].selected) {
        days.push(options[i].value);
    }
}
alert(days);
0
On

Look at the dataItems() function:

http://docs.telerik.com/kendo-ui/api/web/multiselect#methods-dataItems

And then you can iterate the selected items like this:

days.dataItems().forEach(function(element, index, array) {    
    //dataItems() returns the selected items
    // Work on each element, element.value
  });

Here is a jsbin:

http://jsbin.com/vijiq/1/edit?html,js,output