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

Is it possible to change the enabled / disabled status of a dropdownlist through a button?

Hello, I am working with a dropdownlist with the property enabled in false and I wanted to know if it can be changed to true through a button
Here is my code:

@(Html.EJS().DropDownList("GenreId")
                                                    .DataSource(dataManager => dataManager.Url(Url.Action("Genre", "Ext", null, Request.Url.Scheme))
                                                        .Adaptor("UrlAdaptor").CrossDomain(true))
                                                    .Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings
                                                    {
                                                        Text = "Name",
                                                        Value = "GenreId"
                                                    })
                                                    .PopupHeight("200px")
                                                    .Width("200px")
                                                    .Value(Model.GenreId).Enabled(false)
                                                    .Render()
                                        )
<button class="btn btn-success" id="btnChange">Change</button>
<script type="text/javascript">
     $("#btnChange").click(function(){
          var x = document.getElementById("GenreId").ej2_instances[0];
          x.element.disabled = false;
        x.element.readOnly = false;
        x.properties.enabled = true;
});
</script>
I do not know if this is the right way to do it. I hope you can help me.
Thanks in advance


3 Replies

PO Prince Oliver Syncfusion Team March 22, 2019 05:54 AM UTC

Hello Juan, 

Thank you for contacting Syncfusion support. 

Yes, it is possible to change the enabled / disabled status of a DropDownList through a button. All you need to do is use the enabled property in the control and set it as true to enable and false to disable it. You can call the dataBind method to reflect the changes immediately in the control. Please refer the below code. 

$("#btnChange").click(function () { 
  var x = document.getElementById("GenreId").ej2_instances[0]; 
  if (x.enabled) { 
    x.enabled = false; // To disable 
    x.dataBind(); //To affect the changes immediately 
  } else { 
    x.enabled = true; // To enable 
    x.dataBind(); //To affect the changes immediately 
  } 
}); 

Let us know if you have any further queries. 

Thanks, 
Prince 



UN Unknown Syncfusion Team replied to Prince Oliver August 25, 2021 02:27 PM UTC

Thank you! It worked for me.



PO Prince Oliver Syncfusion Team August 26, 2021 07:00 AM UTC

Hi Nicolás, 

Most welcome. We are glad that the solution worked in your end. 

Regards, 
Prince 


Loader.
Live Chat Icon For mobile
Up arrow icon