How to set selected DropDownList value using jQuery

How to change the value of a drop-down list that has data set using a data source

ddlContacts.DataSource = Data; ddlContacts.DataBind(); 

I tried this but it does not work:

 $('#<%= rbDepartment.ClientID %>').change(function() { if ($("input[@name=GroupName]:checked").val() == "IS") { $('#ddlContactType').val('AM'); } }); 
+4
source share
2 answers

Take a picture:

 var selectedValue = $("#<%=ddlContacts.ClientID%> option:selected").val(); 

Just noticed that you are trying to set the value:

 $("#<%=ddlContacts.ClientID%>").val("thevalue"); 

Remember that when working with ASP.NET controls on the client side, you must use the ClientID .

+11
source

I had the same problem getting the currently selected value from the drop down list and setting the new value as the selected one. Below is the code I used and it works:

ASP.NET code:

 <asp:DropDownList runat="server" ID="ddlVersion" /> 

Select the currently selected list using jQuery:

 var selectedVersion = $('#<%=ddlVersion.ClientID%> option:selected').text(); 

To set the selected value in the drop-down list:

 $('#<%=ddlVersion.ClientID%> option:selected').text(currentVersion); 

This code works just fine.

0
source

All Articles