Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Dropdown selected value using JQuery

Tags:

jquery

I use the following code to get the selected value of my dropdown using JQuery.

pStartMonth = $('#cboMonth1').val();

But I get the result as undefined. What am I missing?

The HTML for my dropdown:

<asp:DropDownList ID="cboMonth1" runat="server" AutoPostBack="true" onclick="javascript:shouldsubmit=false;" ValidationGroup="vTimeSlot">
     <asp:ListItem Value="0">-Select-</asp:ListItem>
     <asp:ListItem Value="1">January</asp:ListItem>
     <asp:ListItem Value="2">February</asp:ListItem>
     <asp:ListItem Value="3">March</asp:ListItem>
     <asp:ListItem Value="4">April</asp:ListItem>
     <asp:ListItem Value="5">May</asp:ListItem>
     <asp:ListItem Value="6">June</asp:ListItem>
     <asp:ListItem Value="7">July</asp:ListItem>
     <asp:ListItem Value="8">August</asp:ListItem>
     <asp:ListItem Value="9">September</asp:ListItem>
     <asp:ListItem Value="10">October</asp:ListItem>
     <asp:ListItem Value="11">November</asp:ListItem>
     <asp:ListItem Value="12">December</asp:ListItem>
</asp:DropDownList>
like image 301
MusicLovingIndianGirl Avatar asked Oct 08 '13 09:10

MusicLovingIndianGirl


People also ask

How do I get the selected value of dropdown?

The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.

How show selected value from database in dropdown using jQuery?

Answer: Use the jQuery :selected Selector You can use the jQuery :selected selector in combination with the val() method to find the selected option value in a select box or dropdown list.

How do I get the selected value of multiple dropdowns in jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.

How do I get the selected value and current selected text of a dropdown box using jQuery?

We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.


1 Answers

id attributes of ASP.Net controls are generated server-side, so in your generated HTML, the id would actually be something like _$ctrl0239023930. What you need to use is ClientID like this:

pStartMonth = $('#<%= cboMonth1.ClientID %>').val();
like image 168
Rory McCrossan Avatar answered Nov 05 '22 15:11

Rory McCrossan