Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from kendo combobox

How can I get from kendo combobox value? I always getting undefined on it.. I've used these variants, but they are not worked for me

var selected = $('#typesCombo').data('kendoComboBox').val();
var selected = $('#typesCombo').data('kendoComboBox').value();
var selected = $('#typesCombo').data('kendoComboBox');

And getting error like: Cannot read property 'val' of undefined

Here's my code:

JS:

$('#loadContainer').load("@Url.Action("Load", "Home")" + id);

var selected = $('#typesCombo').data('kendoComboBox').val();

if (selected == '') {
    ...
    }

HTML:

@(Html.Kendo().ComboBoxFor(x => x.Types.Name).Name("typesCombo")
                                             .DataTextField("Name")
                                             .DataValueField("Id")
                                             .HtmlAttributes(new { style = "width:100%", id = "typesCombo" })
                                             .BindTo(Model.TypesList))
like image 814
semper fi Avatar asked Nov 29 '22 23:11

semper fi


2 Answers

There are many ways to get the widget selected value. If you are trying to get the value after it initialization and it has no selected value(declared in the index parameter) you will get an empty value. If you want to get the value when user changes it, you can use the select event and get the value like this:

$("#typesCombo").data('kendoComboBox').value(); // The selected value itself
$("#typesCombo").data('kendoComboBox').dataItem(); // The selected entire dataItem object
$("#typesCombo").val(); // Only if the target element is an input element

Working demo

like image 93
DontVoteMeDown Avatar answered Dec 04 '22 11:12

DontVoteMeDown


You forget use # before id. Try following:

var selected = $("#typesCombo").data('kendoComboBox').value()
like image 36
Mohammad Akbari Avatar answered Dec 04 '22 10:12

Mohammad Akbari