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

Dinamically select item in dropdownlist with javascript

Hi, 
I have a page with a dropdownlist element defined like this:
<div class="form-group">
                    @Html.LabelFor(model => model.IdCiudad, htmlAttributes: new { @class = "control-label col-md-4" })
                    @Html.EJS().DropDownListFor(model => model.IdCiudad).DataSource((IEnumerable<Credito.Models.CiudadViewModel>)ViewBag.Ciudades).Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "Nombre", Value = "IdCiudad" }).Render()
                    @Html.ValidationMessageFor(model => model.IdCiudad, "", new { @class = "text-danger" })
                </div>

But, I need to select a value and show the correspondent text using javascript.
I am using this: var ddlObj1 = new ej.dropdowns.DropDownList({
                        value: $("#IdCiudad").val()
                    });
                    ddlObj1.appendTo("#IdCiudad"); 
but it creates a second dropdownlist over the one already created. 

How can I get the instance of a syncfusion object? It is not as clear as it is in version (EJ1), and documentation is very poor.
Sorry to express my annoyance but, I feel frustrated
I am consulting this urls for documentation:
https://ej2.syncfusion.com/aspnetmvc/documentation/introduction/
https://ej2.syncfusion.com/javascript/documentation/introduction/
but is not clear, not enough, no samples on that. 
There are others links where I could find documentation?

Regards,

Juan J.



1 Reply

PO Prince Oliver Syncfusion Team March 15, 2019 12:31 PM UTC

Hi Juan, 

Thank you for contacting Syncfusion support. 

Query : I need to select a value and show the correspondent text using javascript.  

You can get or select the value using the value property which is accessible via control’s instance. Kindly refer to the following code snippet. 

// Access the control's instance 
var ddlObj1 = document.getElementById("IdCiudad").ej2_instances[0]; 
 
// Use the value property to select a value 
ddlObj1.value = $("#IdCiudad").val(); 
 
//Call the dataBind method to affect the change immediately 
ddlObj1.dataBind(); 
 
Query : How can I get the instance of a syncfusion object? It is not as clear as it is in version (EJ1), and documentation is very poor. 
 
You can also pass the component as second argument in ‘getComponent()’ method to get the instance. Please refer the below standard way to access the component instance.   
var rbObj = ej.base.getComponent(document.getElementById("rdEmp"), ej.buttons.RadioButton);   
rbObj.checked = true;   
  
We will update our documentation about this standard way to access the component and it will be available in our Essential Studio Volume 1, 2019 release.  

Please let us know if you need any further assistance on this.  

Regards, 
Prince 


Loader.
Live Chat Icon For mobile
Up arrow icon