Solved

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

Posted on 2010-11-23
4
270 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This article discusses four methods for overlaying images in a container on a web page
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now