3

how to get id of selected name from dropdown.
whene select Apples then got id 1and select Oranges then 2.
this is simple kendo dropdown example.

<body>
            <input id="dropdownlist" />

            <script>
                $("#dropdownlist").kendoDropDownList({
                  dataSource: [
                    { id: 1, name: "Apples" },
                    { id: 2, name: "Oranges" }
                  ],
                  dataTextField: "name",
                  dataValueField: "id",
                  index: 1,
                  select: onSelect
                });

                function onSelect(e) {
                console.log(e);
                  };
            </script>
</body>

thanks.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
웃웃웃웃웃
  • 362
  • 3
  • 17
  • 32

4 Answers4

6

In order to retrieve the selected Id you can use the dataItem object and access the id within it with change event:

 var dataItem = e.sender.dataItem();
 $('#id').text(dataItem.id);

This will get you access to any data within the object too:

$('#name').text(dataItem.name);

Working example

http://jsfiddle.net/ygBq8/1/

Html

<input id="dropdownlist" /><br/>
<span id="id" >Id</span><br/>
<span id="name" >Name</span><br/>

JavaScript

$("#dropdownlist").kendoDropDownList({
                  dataSource: [
                    { id: 1, name: "Apples" },
                    { id: 2, name: "Oranges" }
                  ],
                  dataTextField: "name",
                  dataValueField: "id",
                  index: 1,
                  change: onChange
                });

                function onChange(e) {
                   var dataItem = e.sender.dataItem();
                   $('#id').text(dataItem.id);
                   $('#name').text(dataItem.name);
                  };
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
3

The Select event is a bit more difficult one to use, as that event fires before the item is selected.

If you use the Change event, you should be able to get the dataItem with

this.dataSource.get(this.value())

See sample http://jsbin.com/OcOzIxI/2/edit

Robin Giltner
  • 3,057
  • 2
  • 18
  • 26
2

Please use this.dataItem()

function onSelect(e) {
    alert(this.dataItem().id);
    alert(this.dataItem().Name);
};
sanme98
  • 189
  • 2
  • 8
1

To select ID of the selected item use:

$("#dropdownlist").val()

And to select TEXT of the selected item use:

$("#dropdownlist").data("kendoDropDownList").text()
Mahdi Ataollahi
  • 4,544
  • 4
  • 30
  • 38