Solved

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

Posted on 2010-11-23
4
274 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
[X]
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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 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