12

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>
2
  • 1
    Please post up your HTML. I'm unable to replicate this. jsfiddle.net/LvsGr Commented Oct 8, 2013 at 9:04
  • Also please post all of your jQuery/Javascript code. Commented Oct 8, 2013 at 9:06

5 Answers 5

18

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();
Sign up to request clarification or add additional context in comments.

Comments

2

The statement you have seems perfectly alright. you might be missing one or more of the following.

  • Include jQuery library
  • Put code in docuemt.ready
  • Ensure you

Edit Based on updated OP, as you have asp.net dropdown the id of dropdown will be changed in generated html so you need to use ClientID. You can also set ClientIDMode to static to generate the same id as you have in server control.

$(document).ready(function(){
   pStartMonth = $('#<%= cboMonth1.ClientID %>').val();
   alert(pStartMonth );
});

ClientIDMode

ASP.NET provides multiple algorithms for how to generate the ClientID property value. You select which algorithm to use for a control by setting its ClientIDMode property. The algorithms are identified by the ClientIDMode enumeration values that are listed in the following table, MSDN.

You can use the server side id in javascript by setting ClientIDMode = "static"

HTML

<asp:DropDownList ID="cboMonth1" runat="server" ClientIDMode="static" AutoPostBack="true" onclick="javascript:shouldsubmit=false;" ValidationGroup="vTimeSlot">

Javascript

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

Comments

2

If the javascript function is in .js file then use:

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

If it is in .aspx file than use:

    $('#<%= cboMonth1.ClientID %>').val();

Comments

1

Try this

$("#cboMonth1 option:selected").val();

1 Comment

This is no different to what the OP has now.
0

If your script is in a file that is not parsed as ASP.Net (such as an included JS file), you can reference the element like this...

pStartMonth = $('[id$=cboMonth1]').val();

That will find an element with an ID that ends with cboMonth1, which yours will.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.