?
Solved

Javascript: Div Display/Hide on Multiple loop through RADIO Buttons

Posted on 2009-04-17
6
Medium Priority
?
843 Views
Last Modified: 2012-08-13
Hi,
I basically have 8 Questions that have YES/NO answers on a form using Radio Buttons for each choice.
<input id="r1" name="r1 value="0" checked onclick="ToggleOff();">NO<input id="r1" name="r1 value="1" onclick="ToggleOn();">YES
<input id="r2" name="r2 value="0" checked onclick="ToggleOff();">NO<input id="r2" name="r2 value="1" onclick="ToggleOn();">YES
<input id="r3" name="r3 value="0" checked onclick="ToggleOff();">NO<input id="r3" name="r3 value="1" onclick="ToggleOn();">YES
etc etc

I am trying to get some javascript working that DISPLAYS/HIDES a DIV based on the floowing rules:
1. Initially ALL questions are 'ckecked' as NO
2. If ANY Quetsion is ticked as YES then the DIV appears
3. If then a Question is ticked as NO (change of mind)  then the script has to checked that there are no other Questions ticked as YES before hidding the DIV.

I want this dynamic on the page - I can do it with page refresh.

The functions below are pretty longwinded and it sort of works, but only once.
When I started the 2 functions all was well, because the Toggle did not check all the other radio values. As soon as I started to check the rest it does not work??


function ToggleOn() { 
	len = document.getElementById('r1').length;
	weareon = 0;
	for(i=0; i<len; i++) {
		if(document.getElementById('r1')[i].checked) {
			if(document.getElementById('r1')[i].value>0) {
				weareon = 1;
			}
		}
	}
	len = document.getElementById('r2').length;
	for(i=0; i<len; i++) {
		if(document.getElementById('r2')[i].checked) {
			if(document.getElementById('r2')[i].value>0) {
				weareon = 1;
			}
		}
	}
	len = document.getElementById('r3').length;
	for(i=0; i<len; i++) {
		if(document.getElementById('r3')[i].checked) {
			if(document.getElementById('r3')[i].value>0) {
				weareon = 1;
			}
		}
	}
 
	if(weareon>0){
		document.getElementById('formfielddec').style.display = 'block'; 
	}
	
}
 
 
function ToggleOff() { 
	len = document.getElementById('r1').length;
	weareon = 0;
	for(i=0; i<len; i++) {
		if(document.getElementById('r1')[i].checked) {
			if(document.getElementById('r1')[i].value>0) {
				weareon = 1;
			}
		}
	}
	len = document.getElementById('r2').length;
	for(i=0; i<len; i++) {
		if(document.getElementById('r2')[i].checked) {
			if(document.getElementById('r2')[i].value>0) {
				weareon = 1;
			}
		}
	}
	len = document.getElementById('r3').length;
	for(i=0; i<len; i++) {
		if(document.getElementById('r3')[i].checked) {
			if(document.getElementById('r3')[i].value>0) {
				weareon = 1;
			}
		}
	}
 
 
 
 
	if(weareon<1){
		document.getElementById('formfielddec').style.display = 'none'; 
	}
 
	
}

Open in new window

0
Comment
Question by:calamaster
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:QualitySoftwareDevelopment
ID: 24166351
Try

document.getElementById('formfielddec').style.visibility= 'visible';

and

document.getElementById('formfielddec').style.visibility= 'hidden';
0
 
LVL 6

Accepted Solution

by:
JoachimMartinsen earned 500 total points
ID: 24166408
You must have a unique ID on ALL items.

Anyways, check this out:
<script>
 
function toggle() {
	countYes = 0;
	for (i=1;i<=8;i++) {
		if (document.getElementById("r"+i+"y").checked) {
			countYes++;
		}
	}
	document.getElementById("mydiv").innerHTML = "Number of yes: " + countYes;
	if (countYes>0) {
		document.getElementById("mydiv").style.visibility = "visible";
	}
	else {
		document.getElementById("mydiv").style.visibility = "hidden";
	}
}
 
</script>
 
 
<input type="radio" id="r1n" name="r1" value="0" checked onclick="toggle();">NO
<input type="radio" id="r1y" name="r1" value="1" onclick="toggle();">YES<br />
 
<input type="radio" id="r2n" name="r2" value="0" checked onclick="toggle();">NO
<input type="radio" id="r2y" name="r2" value="1" onclick="toggle();">YES<br />
 
<input type="radio" id="r3n" name="r3" value="0" checked onclick="toggle();">NO
<input type="radio" id="r3y" name="r3" value="1" onclick="toggle();">YES<br />
 
<input type="radio" id="r4n" name="r4" value="0" checked onclick="toggle();">NO
<input type="radio" id="r4y" name="r4" value="1" onclick="toggle();">YES<br />
 
<input type="radio" id="r5n" name="r5" value="0" checked onclick="toggle();">NO
<input type="radio" id="r5y" name="r5" value="1" onclick="toggle();">YES<br />
 
<input type="radio" id="r6n" name="r6" value="0" checked onclick="toggle();">NO
<input type="radio" id="r6y" name="r6" value="1" onclick="toggle();">YES<br />
 
<input type="radio" id="r7n" name="r7" value="0" checked onclick="toggle();">NO
<input type="radio" id="r7y" name="r7" value="1" onclick="toggle();">YES<br />
 
<input type="radio" id="r8n" name="r8" value="0" checked onclick="toggle();">NO
<input type="radio" id="r8y" name="r8" value="1" onclick="toggle();">YES<br />
 
<div id="mydiv" style="visibility: hidden;">Number of yes: 0</div>

Open in new window

0
 
LVL 1

Author Comment

by:calamaster
ID: 24166426
Hi, thanks for the response.
However this is not the problem. This bit of the code works fine, it is the functions that do not work.
0
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!

 
LVL 6

Expert Comment

by:JoachimMartinsen
ID: 24166522
So why not use my "not so longwinded" function instead?
0
 
LVL 1

Author Closing Comment

by:calamaster
ID: 31571392
Excellent code - worked a treat. I did customise a bit but the barebone principles and function was very helpful.
Thanks
0
 
LVL 1

Author Comment

by:calamaster
ID: 24166576
Joachim,
My comment was for the first response. Yours worked a treat!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

807 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