Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-24-2011, 03:40 AM   PM User | #1
kreut
New Coder

 
Join Date: Feb 2011
Location: Bellingham, WA
Posts: 31
Thanks: 11
Thanked 0 Times in 0 Posts
kreut is an unknown quantity at this point
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 />
kreut is offline   Reply With Quote
Old 02-24-2011, 04:11 AM   PM User | #2
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
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.
bullant is offline   Reply With Quote
Old 02-24-2011, 11:54 AM   PM User | #3
kreut
New Coder

 
Join Date: Feb 2011
Location: Bellingham, WA
Posts: 31
Thanks: 11
Thanked 0 Times in 0 Posts
kreut is an unknown quantity at this point
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....
kreut is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:54 PM.


Advertisement
Log in to turn off these ads.