...

View Full Version : Using jquery to show/hide with radio buttons



kreut
02-24-2011, 03:40 AM
Hello,

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

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:


$(document).ready(function(){
$(':radio').click(function(){
if ($(this).attr('checked')) {
$('.answer').show();
} else{
$('.answer').hide();}
})
}
)
My html:

<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 />

bullant
02-24-2011, 04:11 AM
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.

kreut
02-24-2011, 11:54 AM
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....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum