Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Show and Hide 2 or more divs using Jquery on a radio button selection

Posted on 2010-11-23
4
273 Views
Last Modified: 2012-06-27
Hi Experts,

I want to show and hide 2 more divs based on radio button selections using jquery

I download the lastest jquery.js file (v1.4.4) and included in the head tag

here is what I am trying to do

first: I have a radio button like
<input type="radio" id="mono" name="Product_Attributes[&mvt:attr:pos;]:value" value="&mvt:option:code;" CHECKED>&mvt:option:prompt;

Open in new window


now I am using the input id="mono" in the javascript like below to show or hide my first div called showmono

<script language="javascript" type="text/javascript">
$('input[id=mono]').click(function() {
    if($(this).val() == "No") {
        $('#showmono').hide();
    } else {
        $('#showmono').show();
    }
});
</script>

Open in new window


but when i run it and select yes , the showmono div does not show up

second: based on step 1 selection, I have one more radio button with four options inside the div showmono
which are as follows
<input type="radio" id="mono" name="Product_Attributes[&mvt:attr:pos;]:value" value="&mvt:option:code;" CHECKED>&mvt:option:prompt;

Open in new window

and its corrosponding script is
<script language="javascript" type="text/javascript">
$('input[id=mono]').click(function() {
    if($(this).val() == "01" || "02") {
        $('#showblocktextbox').show();
		$('#showscripttextbox').hide();
    } else if(($(this).val() == "03" || "04"){ 
	     $('#showscripttextbox').show();
		 $('#showblocktextbox').hide();
    } else {
	      $('#showscripttextbox').hide();
		 $('#showblocktextbox').hide();
    }
	   
});
</script>

Open in new window


How can I achieve this ,any help is appreciated it
Thanks


0
Comment
Question by:niceoneishere
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34198854
You should not have multiple radio buttons with the same id i.e. 'mono'. Try to give unique ids to the buttons. To select the second set of radio buttons together give them the same class. Also change your code as (replace id with the class, say, radio2):


$('.radio2').click(function() {
    if($(this).val() == "01" || $(this).val() == "02") {
        $('#showblocktextbox').show();
		$('#showscripttextbox').hide();
    } else if(($(this).val() == "03" || $(this).val() == "04"){ 
	     $('#showscripttextbox').show();
		 $('#showblocktextbox').hide();
    } else {
	      $('#showscripttextbox').hide();
		 $('#showblocktextbox').hide();
    }
	   
});

Open in new window

0
 
LVL 1

Author Comment

by:niceoneishere
ID: 34199315
Thanks for replying I  have changed the step one code to a checkbox and here is my new code
<script  type="text/javascript">
$(document).ready(function(){
		
		//Hide div w/id monoradio
	   $("#monoradio").css("display","none");

		// Add onclick handler to checkbox w/id checkme
	   $("#checkme").click(function(){
		
		// If checked
		if ($("#checkme").is(":checked"))
		{
			//show the hidden div
			$("#monoradio").show("fast");
		}
		else
		{	   
			//otherwise, hide it 
			$("#monoradio").hide("fast");
		}
	  });
	
		});


</script>

Open in new window

the code now works when check the checkbox, it shows the div but when i uncheck its not hiding the div

Thanks



0
 
LVL 16

Expert Comment

by:jmatix
ID: 34199632
If you just want to toggle between checked and not checked statuses, a checkbox would be more appropriate than a radio button because browser will automatically toggle the status on each click. For radio buttons it won't do that automatically. Radio buttons are suitable when you want to select one option from a number of options.
0
 
LVL 1

Author Closing Comment

by:niceoneishere
ID: 34234029
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 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