Reputation: 149
I know that this question has been asked several times but I'm not able to solve this problem. My problem is simple how we get asp dropdown id using jQuery.
ASP
<asp:DropDownList ID="ddlNewPortfolioName" Width="150" runat="server" AutoPostBack="True"></asp:DropDownList>
JS
alert($("[id$='<%=ddlPortfolioName.ClientID %>']").val());
This logic not work for me it show undefined value, whether there are some value available in dropdown
and out of these first one is selected. Please help me on this
Upvotes: 1
Views: 5183
Reputation: 316
You can try like dis
Var data=$("input[id*=ddlPortfolioName]").val();
alert(data);
or
alert($('#<%= ddlPortfolioName.ClientID %>').val());
or if its in child page..den u hv to pass the server side ID..for example my server side id is
alert($("#ctl00_MainContent_ddlPortfolioName").val());
Upvotes: 0
Reputation: 82251
You need to select selected option and then get its value.use:
$('#<%=ddlPortfolioName.ClientID %> option:selected').val()
Upvotes: 0
Reputation: 148170
Simply use ID Selector (“#id”) you do not need attribute selector with wild card if you have only one item to get. As ClientID
gives you complete and exact id of element.
alert($('#<%= ddlPortfolioName.ClientID %>').val());
If you are using framework 4 or above you can use Control.ClientIDMode to keep the Server id as ClientID
alert($('#ddlPortfolioName').val());
If you have dropdown in grid or repeater or listView then you will have to use contains wild card with attribute selector.
$('[id*=ddlPortfolioName]').each(function(){
alert($(this).val());
});
Upvotes: 4