Solved

Can't set the selected option for dropdown using JQuery

Posted on 2013-06-21
8
1,290 Views
Last Modified: 2013-06-21
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!
0
Comment
Question by:dyarosh
  • 4
  • 3
8 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Use : $("#ddPromotionReason").val("");

Test page : http://jsfiddle.net/GPVQJ/
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
Here you go:

$('#ddPromotionReason option:first-child').attr("selected", "selected");

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
work too : $("#ddPromotionReason option:first").attr("selected", "selected");
0
 

Author Closing Comment

by:dyarosh
Comment Utility
Thank you!!
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
@leakim971 - Using val() is fine if you know the value you want to select, but not if you just want the first option selecting...
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
I believe he knowwhich value he want reading its try :
$("#ddPromotionReason option[value='\"\"']")

anyway, you got it, congrats, see you ;-)
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
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 ;)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now