We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to select Default value through Methods?

Hi Syncfusion,

I have a question

  • How to select value through another function for ejDropDownList?

HTML:

<div class="control">
   <div class="ctrllabel">Select a bike</div>
   <input type="text" id="bikeList" />
</div>

JavaScript:

<script type="text/javascript">
        var target;
        $(function () {
            // declaration
            BikeList = [
                 { empid: "bk1", text: "Apache RTR" }, { empid: "bk2", text: "CBR 150-R" }, { empid: "bk3", text: "CBZ Xtreme" },
                 { empid: "bk4", text: "Discover" }, { empid: "bk5", text: "Dazzler" }, { empid: "bk6", text: "Flame" },
                 { empid: "bk7", text: "Fazzer" }, { empid: "bk8", text: "FZ-S" }, { empid: "bk9", text: "Pulsar" },
                 { empid: "bk10", text: "Shine" }, { empid: "bk11", text: "R15" }, { empid: "bk12", text: "Unicorn" }
            ];           

            $('#bikeList').ejDropDownList({
                dataSource: BikeList,
                fields: { id: "empid", text: "text", value: "empid" }
            });
           
       
           // this code in another function
           var dropDown = $("#bikeList").data("ejDropDownList");
           dropDown.selectItemByValue("bk7") // doesn't work

           dropDown.selectItemByText("Fazzer") // doesn't work

         });
    </script>


thanks in advance



1 Reply

HP Harikrishnan P Syncfusion Team May 5, 2015 01:37 PM UTC

Hi Edgar,

Thanks for using Syncfusion products.

Query: How to select value through another function for ejDropDownList?

We would like to inform you that, there are no public methods as “selectItemByValue” and “selectItemByText”. To set the value to EJ Dropdownlist, use “setSelectedValue” or “setSelectedText” methods.

setSelectedValue():

This method selects the dropdownlist item, if the specified value matches with the ‘value’ of dropdownlist items. In case, while specifying the ‘fields’ property if you have not specified the ‘value’ field means, then dropdownlist will select the correspondingitem, if the value specified in “setSelectedValue()” matches with the ‘text’ of the dropdownlist items.

setSelectedText():

This method selects the drodownlist item, if the value specified in “setSelectedText()” matches with the ‘text’ of the dropdownlist items.

You can also use ‘value’ or ‘text’ property to select the item in dropdownlist. Please refer the below code snippet.


<script type="text/javascript">
var dropDownObj;
$(function () {
// declaration
BikeList = [
{ empid: "bk1", text: "Apache RTR" }, { empid: "bk2", text: "CBR 150-R" }, { empid: "bk3", text: "CBZ Xtreme" },
{ empid: "bk4", text: "Discover" }, { empid: "bk5", text: "Dazzler" }, { empid: "bk6", text: "Flame" },
{ empid: "bk7", text: "Fazzer" }, { empid: "bk8", text: "FZ-S" }, { empid: "bk9", text: "Pulsar" },
{ empid: "bk10", text: "Shine" }, { empid: "bk11", text: "R15" }, { empid: "bk12", text: "Unicorn" }
];
$('#bikeList').ejDropDownList({
dataSource: BikeList,
//'empid' attribute is mapped to the 'value' field.
fields: { id: "empid", text: "text", value: "empid" }
});
});

function setValue() {
//Object for Dropdownlist is created
dropDownObj = $("#bikeList").data("ejDropDownList");
//Set value to EJ Dropdownlist using public methods
dropDownObj.setSelectedValue("bk7"); // Set value in drodownlist based on 'value' field.
dropDownObj.setSelectedText("FZ-S"); // Set value in dropdownlist based on 'text' field.
//Set value to EJ Dropdownlist using properties.
dropDownObj.option("value", "bk7"); //Sets value to dropdownlist based on the field mapped to 'value' field. In case if 'value' field is not specified, value will be set to dropdownlist if the specified value matches the text of the dropdownlist items.
dropDownObj.option("text", "Dazzler"); //Sets value to dropdownlist, if the text matches with text of dropdownlist items.
}
script>




You can refer the following class reference link for JavaScript to about the list of Properties, methods and events supported by each component.

http://help.syncfusion.com/cr/js

Please let us know if you have further queries.

Regards,
HariKrishnan

Loader.
Live Chat Icon For mobile
Up arrow icon