Can't set the selected option for dropdown using JQuery

Posted on 2013-06-21
Medium Priority
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>

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!
Question by:dyarosh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 82

Expert Comment

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

Test page : http://jsfiddle.net/GPVQJ/
LVL 43

Accepted Solution

Chris Stanyon earned 2000 total points
ID: 39265565
Here you go:

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

Open in new window

LVL 82

Expert Comment

ID: 39265568
work too : $("#ddPromotionReason option:first").attr("selected", "selected");
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.


Author Closing Comment

ID: 39265610
Thank you!!
LVL 82

Expert Comment

ID: 39265632
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
LVL 43

Expert Comment

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

Expert Comment

ID: 39265666
I believe he knowwhich value he want reading its try :
$("#ddPromotionReason option[value='\"\"']")

anyway, you got it, congrats, see you ;-)
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39265673
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 ;)

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

770 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