views:

363

answers:

2

Hi, I have one control with some asp form elements, but how do I get the form elements in my other control. I usually do jQuery('#<%= MyDropDownList.ClientID %>).val(), but this is not possible since it is in the other control. How do I access the element without hardcoding it?

So to sum it up: Control 1: Asp:DropDownList Control 2: Needs access to the value in the asp:DropDownlist from control 1 through jQuery.

+1  A: 

You can get value for the dropdown inside Control1 using the the following jQuery selector.

$('#<%= Control1.ClientID%> select').val()
Jose Basilio
I know the value inside control1 can be get by the code you wrote, but what if I want it from control 2?
Dofs
I am assuming that both controls are in the same page and the jQuery is part of the page. If the jQuery is part of each individual control, then you may to assign a unique CssClass property to the dropdown in Control1 that you can uniquely identify it from Control2.
Jose Basilio
If this does not help, please post some of your code code.
Jose Basilio
+1  A: 

If the names are unique, you can use the selector does name matching on the end of the id.

$('[id$="DropDownList1"]').val();

This will match all controls whose id ends with DropDownList1 and get the value of the first one. If the name is unique, then it will be your other dropdown list.

tvanfosson