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').at tr("disabl ed", true);
// Remove option selected
$("#ddPromotionReason option[value='\"\"']").att r("selecte d", "selected");
Any help is greatly appreciated!
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
<asp:ListItem Value="">-- Choose Promotion Reason --</asp:ListItem>
</asp:DropDownList>
Here is the JQuery:
// Disable Promotion Reason DropDown if Promotion Date removed
$('#ddPromotionReason').at
// Remove option selected
$("#ddPromotionReason option[value='\"\"']").att
Any help is greatly appreciated!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
work too : $("#ddPromotionReason option:first").attr("selec ted", "selected");
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
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 ;-)
$("#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 ;)
Test page : http://jsfiddle.net/GPVQJ/