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

Previously selected value in dropdown list

Hello,
is it possible to somehow get the previously selected value in dropdown list on change event? 
I have a dropdown list with several possible options to choose from. On change event, if the previously selected value was 1, I have to reset some fields on form; otherwise, I do not have to reset those fields. Currently I do not see the way to achieve this with Syncfusion's control. 

3 Replies

PO Prince Oliver Syncfusion Team February 19, 2019 08:42 AM UTC

Hi Petra, 
 
Thank you for contacting Syncfusion support.  
 
We have checked your requirement to get previously selected values through change event of DropDownList,  currently we cannot get previous value in change event. However, we can achieve your requirement in the following ways.  
 
  • Method 1:
Bind click event for DropDownList container and store the DropDownList value through this event using a global variable. This variable can be accessed in change event of DropDownList after value changes as shown below. 
 
<ej-drop-down-list id="bikeList" datasource="(IEnumerable<Bikes>)ViewBag.datasource" watermark-text="Select a bike" width="100%" change="onValueChange" > 
        <e-drop-down-list-fields  text="text" value="value" /> 
    </ej-drop-down-list> 
 
  <script> 
        var ddlObj,PreviousVal; 
        $(function () { 
            
            $("#bikeList_container").on("click", function () { 
                ddlObj = $("#bikeList").data("ejDropDownList"); 
                PreviousVal = ddlObj.value(); 
            }); 
 
        }); 
        function onValueChange(args) { 
            if (PreviousVal == 1) { 
                alert("Previous Value before change:" + PreviousVal); 
                //reset the required form fields here if the value is 1. 
            } 
 
        } 
 
    </script> 
 
 
 
  • Method 2:
If you are setting value dynamically without interaction with the component, then you can declare a global variable and assign the value through create event of DropDownList initially. Later this value can be updated through change event of DropDownList for each value change.  
 
<form id="form1"> 
    <ej-drop-down-list id="bikeList" datasource="(IEnumerable<Bikes>)ViewBag.datasource" watermark-text="Select a bike" width="100%" change="onValueChange" create="onCreate"> 
        <e-drop-down-list-fields  text="text" value="value" /> 
    </ej-drop-down-list> 
    <button id="set" onclick="setValue()" type="button">Set Value dynamically </button> 
   
</form> 
    <script> 
        var ddlObj,PreviousVal; 
        function setValue() { 
            ddlObj = $("#bikeList").data("ejDropDownList"); 
            ddlObj.setModel({ value: "1" }); 
        } 
        function onCreate(args) { 
 
            PreviousVal = this.value(); 
        } 
       
        function onValueChange(args) { 
            if (PreviousVal == 1) { 
                alert("Previous Value before change:" + PreviousVal); 
                    //reset the required form fields here if the value is 1 
            } 
            PreviousVal = this.value(); 
 
        } 
 
    </script> 
 
 
You can use any one of the above methods based on your convenience. We have attached a sample for your reference, please find the sample at the following location: http://www.syncfusion.com/downloads/support/forum/142758/ze/PreviousSelectedValue-268518768  
 
Please let us know if you need any further assistance on this. 
 
Regards, 
Prince 



PV Petra Visic February 19, 2019 08:52 AM UTC

Thanks! I am familiar with the method similar to what you have described, I just hoped that your dropdown list control had built-in member for that...


PO Prince Oliver Syncfusion Team February 21, 2019 11:11 AM UTC

Hi Petra, 

Thank you for the update. 

The previously suggested solution will work fine in all cases and we kindly request you to use the same in your end to achieve your requirement. 

Regards, 
Prince 


Loader.
Live Chat Icon For mobile
Up arrow icon