Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2011
    Location
    Bellingham, WA
    Posts
    31
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Using jquery to show/hide with radio buttons

    Hello,

    This is my first week with javascript and I think that I'm already convinced that jquery is the way to go.

    I'm trying to write jquery code that will show answers to questions when a radio button is clicked, and make the answers disappear if it's not. Someone was kind enough to show me a javascript solution, but I with jquery, a more elegant solution is possible. I've attempted the code, but I fear that nothing actually happens! Any help would be appreciated:

    Code:
    $(document).ready(function(){           
    $(':radio').click(function(){
    	if ($(this).attr('checked')) {
    		$('.answer').show();
    		} else{
    		$('.answer').hide();}
    })
    }
    )
    My html:
    Code:
    <form action="" method="post" name="Question1"> <label>
          <input type="radio" name="ex1" value="wrong1" id="ex1"/>
          <span class="answer1" lang="latex">x^{3}</span><span class="answer">THis should print if this button is selected</span></label>
        <br />
        <label>
          <input type="radio" name="ex1" value="wrong2" id="ex2" />
          <span class="answer2" lang="latex">2x+1</span><span class="answer">This should print if this button is selected</span></label>
        <br />

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    imo, since you're just starting to learn javascript you will be better off learning the fundamentals of javascript first and doing simple things like this with plain javascript before playing with jQuery.

    I see too many people starting out by trying to take a jQuery shortcut and not learning javascript basics first and they then end up spending a lot of time in forums like this looking for help when they get stuck.

    My suggestion would be to learn how to do it in plain javascript, which is fairly straight forward for this basic task and then move on to jQuery if you like when you have a reasonable grasp of javascript basics.

  • #3
    New Coder
    Join Date
    Feb 2011
    Location
    Bellingham, WA
    Posts
    31
    Thanks
    11
    Thanked 0 Times in 0 Posts
    I appreciate your advice. And, while I still would very much like to see how this would work using jquery (anybody?), following your suggestion, "Does anybody have a good suggestion for an intro javascript book?"

    Thanks so much....


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •