Why the getSelectedValue method always returns the value in string, even the value in some other data type? Is there any way to achieve that?
Description
By default, getSelectedValue method is used to retrieve the items value that are selected in the DropDownList. It returns the item value in the “string” data type instead of corresponding data type of the item.
Solution
But, using itemValue property, we can get the selected items as it is. But the “itemValue” property is only applicable for Single Selection mode in the DropDownList.
In the following example, upon selecting the items in the DropDownList, the change event will be triggered. In the change event, itemValue will returns the selected value in the DropDownList and the returned value will be in its corresponding data type.
For reference, we have added the local method called “toType” which receives the selected value from the DropDownList and returns the data type of the value.
Please refer the following code snippet.
Javascript
<input type="text" id="bikeList" /> <script type="text/javascript"> var target; $(function () { BikeList = [ { value: 2, text: "CBR 150-R" }, { value: "hello", text: "CBZ Xtreme" }, { value: 4, text: "Discover" }, { value: 5, text: "Dazzler" }, { value: 6, text: "Flame" }, { value: "hi", text: "Fazzer" }, { value: 8, text: "FZ-S" }, { value: 9, text: "Pulsar" }, { value: 10, text: "Shine" }, { value: 11, text: "R15" }, { value: 12, text: "Unicorn" } ]; var ddl = $('#bikeList').ejDropDownList({ dataSource: BikeList, change: function (args) { var rteValue = args.model.itemValue; alert("Type :" + toType(rteValue) + " Value :" + (rteValue)); alert("Selected value :" + ddl.getSelectedValue()); } }).data("ejDropDownList"); }); var toType = function (obj) { return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase() } </script>