3

I have been trying to get the selected item value from a dropdownlist but it seems like it is not going to work. I have looked at the other topics and I know that this question has been asked several times, but I need help. I have tried the following code:

$('ddlWorkHourFact option:selected').val()

but it returns me "undefined" and I don't know why.

Here is my dropdownlist:

<asp:DropDownList ID="ddlWorkHourFact"  runat="server">
                            <asp:ListItem Value="7" Text="7">7</asp:ListItem>
                            <asp:ListItem Value="8" Selected="True" Text="8">8</asp:ListItem>
                            <asp:ListItem Value="9" Text="9">9</asp:ListItem>
                        </asp:DropDownList>
6
  • 2
    You're missing the # before the ddlWorkHourFact. Probably is that. Also, check the ID generated by ASP.NET in client's browser. Commented Dec 27, 2011 at 10:29
  • nop it still returns undefined Commented Dec 27, 2011 at 10:32
  • Yep, check the ID as I've mentioned below. ASP.NET generates it's own IDs. Commented Dec 27, 2011 at 10:32
  • I also tried that and the id is "ContentPlaceHolder1_ddlWorkHourFact". it returns undefined still Commented Dec 27, 2011 at 10:33
  • Did you try it with the # in front of the id? and without the :selected as I wrote below? Commented Dec 27, 2011 at 10:40

6 Answers 6

14

First, make sure that the ID ddlWorkHourFact exists in the HTML code. ASP.NET often creates something like this: ctl1_ddlWorkHourFact. You can use

ClientIDMode="Static"

to avoid that problem. Afterwards

$('#ddlWorkHourFact').val()

should be enough.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, there are many instances where you can't set the ClientIDMode to Static. I've found many third-party tools for ASP.Net do not like this approach.
6

Ktt,

You need to understand that ASP.NET will generate a different ID in client-side if you are putting the control inside a content, etc. This is done because the ID in client-side should be unique. If you are using ASP.NET 4.0 you can do what Remy told you.

If you are not using ASP.NET 4.0, you can't do that, but you can do a "workaround" in jquery.

function GetClientID(id, context) {
var el = $("#" + id, context);
if (el.length < 1)
el = $("[id$=_" + id + "]", context);
return el;
}

For more information on this code you can go here.

Then, you only need to do something like: GetClientID("txtboxId").attr("id") to get the ID.

$("#" + GetClientID("ddlWorkHourFact").attr("id") + " option:selected").val();

This is only a example, you can improve the code.

Edit:

You can also use something like this, if you are doing that in same page of control.

$('#<%= ddlWorkHourFact.ClientID %> option:selected').val();

Comments

5

try $('#ddlWorkHourFact :selected').val()

seems like you are missing the hash "#" symbol...

1 Comment

none of the answer has corrected. it still returns "undefined"
1

try something like

$('#ddlWorkHourFact').options[$('#ddlWorkHourFact').selectedIndex].val();

2 Comments

okay I figured the problem. I was trying to alert it when the page loads. script was running when the page starts. but I put the alert in a function and give it as an onclick, then the problem is over now. Thanks for helps
I guess the dropdown doesnt get loaded before alert. that's why I got the problem.
1

try this :

   var _ddl = $("[id$='_ddlWorkHourFact']").attr("id");
    _option = "#" + _ddl + " option:selected";
    _value= $(_option).val();

Comments

0

okay I figured the problem. I was trying to alert it when the page loads. script was running when the page starts. but I put the alert in a function and give it as an onclick, then the problem is over now. Thanks for helps

1 Comment

This is a duplicate of the comment you made last week.

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.