Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

Can't set the selected option for dropdown using JQuery

I have a web form that contains a textbox and a dropdown list.  The dropdown list is disabled unless there is a value in the textbox when the page loads.  This is done in the code behind.  Once the page is loaded, I use JQuery to enable and disable the textbox based on whether there is data in the textbox.  I have that working fine.  

What I need to do is reset the selected option to the first option in the list which is my Choose prompt with a value of "".  I can't seem to get the JQuery correct.  Here is the dropdown definition:

                        <asp:DropDownList ID="ddPromotionReason" runat="server"
                            AppendDataBoundItems="True" CausesValidation="True" ClientIDMode="Static" Enabled="False">
                                <asp:ListItem Value="">-- Choose Promotion Reason --</asp:ListItem>
                        </asp:DropDownList>

Here is the JQuery:

                    // Disable Promotion Reason DropDown if Promotion Date removed
                    $('#ddPromotionReason').attr("disabled", true);
                    // Remove option selected
                    $("#ddPromotionReason option[value='\"\"']").attr("selected", "selected");

Any help is greatly appreciated!
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Use : $("#ddPromotionReason").val("");

Test page : http://jsfiddle.net/GPVQJ/
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
work too : $("#ddPromotionReason option:first").attr("selected", "selected");
Avatar of dyarosh
dyarosh

ASKER

Thank you!!
From the main site : http://api.jquery.com/val/

Locate this section :
Example: Set a single select, a multiple select, checkboxes and a radio button .

Line 26
@leakim971 - Using val() is fine if you know the value you want to select, but not if you just want the first option selecting...
I believe he knowwhich value he want reading its try :
$("#ddPromotionReason option[value='\"\"']")

anyway, you got it, congrats, see you ;-)
What I need to do is reset the selected option to the first option in the list...
More than one way to skin a cat ;)