Solved

display div if selected item in dropdown

Posted on 2013-01-18
2
671 Views
Last Modified: 2013-01-18
Hello,

I have tried to run this code to try display the div if Session is selected
But no matter what's selected it always shows the "#answer_wrapper" div
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
jQuery("#Type").change(function () {
    var id = jQuery(this).find("option:selected").attr("value");
    if (id == "Session") {
        jQuery("#answer_wrapper").show();
    }
    else {
        jQuery("#answer_wrapper").hide();
    }
});
</script> 
<asp:DropDownList ID="Type" runat="server">
              <asp:ListItem Value="SteadyRun">Steady Run</asp:ListItem>
              <asp:ListItem Value="EasyRun">Easy Run</asp:ListItem>
              <asp:ListItem Value="LongRun">Long Run</asp:ListItem>
              <asp:ListItem Value="Session">Session</asp:ListItem>
    </asp:DropDownList>
<div id="answer_wrapper">
    some code here
</div>

Open in new window

0
Comment
Question by:runnerjp2005
  • 2
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38792666
you need to wait DOM ready before to play with the object inside
test page : http://jsfiddle.net/m5UxK/
jQuery(document).ready(function() {

jQuery("#Type").change(function () {
    var id = jQuery(this).find("option:selected").attr("value");
    if (id == "Session") {
        jQuery("#answer_wrapper").show();
    }
    else {
        jQuery("#answer_wrapper").hide();
    }
});


})
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
ID: 38792678
another one to fire the event at page load :  http://jsfiddle.net/m5UxK/1/
    jQuery(document).ready(function() {
        jQuery("#Type").change(function () {
            var id = jQuery(this).find("option:selected").attr("value");
            if (id == "Session") {
                jQuery("#answer_wrapper").show();
            }
            else {
                jQuery("#answer_wrapper").hide();
            }
        }).change();
    })

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)

726 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