Solved

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

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

630 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