?
Solved

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

Posted on 2010-11-23
4
Medium Priority
?
282 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:
Justin Mathews earned 2000 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:Justin Mathews
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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).

719 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