kendo ComboBox find and select value if exists

mspasiuk picture mspasiuk · Jun 17, 2014 · Viewed 13.5k times · Source

I have a kendo combobox, and also have a variable with a value. How can I search for the value in the combobox, and if exists, select it? All with javascript/Jquery

http://jsfiddle.net/mspasiuk/8HnnZ/

var movieId=10;


var combo =$("#movies").kendoComboBox({
    dataTextField: "text",
    dataValueField: "value",
    dataSource: data,
    height: 100
});

I can't find how to set the value having and Id. So my question is how to set the selectedIndex or Value to the one I have, and if is not exists, set the selectedIndex to -1

Answer

Muhammad Umar picture Muhammad Umar · Dec 23, 2014

1) You need to subscribe for change event first to accommodate changed value.

var widget = $("#movies").data("kendoComboBox");     

2) In change event you can now code for find and select existing value.

widget.bind("change", function() {
  if (widget.selectedIndex === -1 && widget.value()) {
      if (widget.dataSource.view().length > 0) {
          widget.select(0)  
      } else {
          widget.value("");                          
      }
  }
});

Click Here to see the demo.