Solved

display div if selected item in dropdown

Posted on 2013-01-18
2
666 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)

743 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

11 Experts available now in Live!

Get 1:1 Help Now