Jump to content

[SOLVED] jQuery - Using the radio button to show/hide a div.


suttercain

Recommended Posts

Hi everyone,

 

I want to be able to show a div cotnainer based on the radio button selected. Example 3 options Yes, No, Don't Know. If yes is clicked a DIV is hown (I have this part working), but if Yes is clicked and the user changes it to No or Don't Know, I want to hide that DIV. This is the part I don't have working, the hide.

 

Here is the code I am using that work with show, but not hide.

 

  $("#14a").click(function(){

	// If checked
	if ($("#14a").is(":radio"))
	{
		//show the hidden div
		$("#extra").show("fast");
	}
	else
	{	   
		//otherwise, hide it 
		$("#extra").hide("fast");
	}
  });

<input id="14a" type="radio" name="14" value="Yes" class="style1" /> Yes<br />
<input id="14b" type="radio" name="14" value="No" class="style1" /> No<br />
<input id="14c" type="radio" name="14" value="Don't Know" class="style1" /> Don't Know
<div id="extra">
<br /><label>If yes, please specify.</label>	
<input id="email" type="text" class="style" />
</div>

 

Any help would be much appreciated. Thank you.

Link to comment
Share on other sites

Well the reason is that you only listen to 'onclick' event of the radiobutton with the id '14a'.

So clicking on another radio button wont fire your event.

 

Try changing

  $("#14a").click(function()

to

  $("#14a").change(function()

(Guessing the name would simply be change in jquery)

If that doesn't work make the function external and add a click event listener to 14b/14c as well, calling the new external function

Link to comment
Share on other sites

Then use the alternative solution I posted. Did post two as I feared changes made my javascript wouldn't be detected.

  $("#14a").click(function(){ checkRb() });
  $("#14b").click(function(){ checkRb() });
  $("#14c").click(function(){ checkRb() });
function checkRb()
{      
      if ($("#14a").is(":radio"))
      {
         //show the hidden div
         $("#extra").show("fast");
      }
      else
      {      
         //otherwise, hide it
         $("#extra").hide("fast");
      }
}

 

 

Link to comment
Share on other sites

Give each input element some static class name like this -

<input id="14a" type="radio" name="14" value="Yes" class="static_class style1" /> Yes<br />
<input id="14b" type="radio" name="14" value="No" class="static_class style1" /> No<br />
<input id="14c" type="radio" name="14" value="Don't Know" class="static_class style1" /> Don't Know
<div id="extra">
<br /><label>If yes, please specify.</label>   
<input id="email" type="text" class="style" />
</div>

<script type="text/javascript">
$(".static_class").click(function(){if($(this).val()==="Yes") $("#div").show("fast"); else $("#div").hide("fast");});
</script>

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.