Link to home
Start Free TrialLog in
Avatar of calamaster
calamasterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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

Avatar of QualitySoftwareDevelopment
QualitySoftwareDevelopment

Try

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

and

document.getElementById('formfielddec').style.visibility= 'hidden';
ASKER CERTIFIED SOLUTION
Avatar of JoachimMartinsen
JoachimMartinsen
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of calamaster

ASKER

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.
So why not use my "not so longwinded" function instead?
Excellent code - worked a treat. I did customise a bit but the barebone principles and function was very helpful.
Thanks
Joachim,
My comment was for the first response. Yours worked a treat!