Member Avatar for gezzalou

Hi,

I am having trouble getting some javascript code to work correctly in mozilla. Shows fine in IE but only works in mozilla one something is changed. The code is simple as i am a beginner at this.

Please see code below.

Thanks in advance

<script language="javascript" type="text/javascript">


function ChangeQ2() {
	if (document.getElementById("_Q0_Q0_Q0_C").value == "__Other") {
		document.getElementById("_Q0_Q0_Q1").style.display = 'inline';
	} else {
		document.getElementById("_Q0_Q0_Q1").style.display = 'none';
		document.getElementById("_Q0_Q0_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q1_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q1_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q1_Q1").style.display = 'none';
			document.getElementById("_Q0_Q1_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q2_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q2_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q2_Q1").style.display = 'none';
			document.getElementById("_Q0_Q2_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q3_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q3_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q3_Q1").style.display = 'none';
			document.getElementById("_Q0_Q3_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q4_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q4_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q4_Q1").style.display = 'none';
			document.getElementById("_Q0_Q4_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q5_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q5_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q5_Q1").style.display = 'none';
			document.getElementById("_Q0_Q5_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q6_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q6_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q6_Q1").style.display = 'none';
			document.getElementById("_Q0_Q6_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q7_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q7_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q7_Q1").style.display = 'none';
			document.getElementById("_Q0_Q7_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q8_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q8_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q8_Q1").style.display = 'none';
			document.getElementById("_Q0_Q8_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q9_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q9_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q9_Q1").style.display = 'none';
			document.getElementById("_Q0_Q9_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q10_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q10_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q10_Q1").style.display = 'none';
			document.getElementById("_Q0_Q10_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q11_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q11_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q11_Q1").style.display = 'none';
			document.getElementById("_Q0_Q11_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q12_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q12_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q12_Q1").style.display = 'none';
			document.getElementById("_Q0_Q12_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q13_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q13_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q13_Q1").style.display = 'none';
			document.getElementById("_Q0_Q13_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q14_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q14_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q14_Q1").style.display = 'none';
			document.getElementById("_Q0_Q14_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q15_Q0_C").value == "__Other") {
			document.getElementById("_Q0_Q15_Q1").style.display = 'inline';
		} else {
			document.getElementById("_Q0_Q15_Q1").style.display = 'none';
			document.getElementById("_Q0_Q15_Q1").value = "";
	}

	if (document.getElementById("_Q0_Q16_Q0_C").value == "__Other") {
				document.getElementById("_Q0_Q16_Q1").style.display = 'inline';
			} else {
				document.getElementById("_Q0_Q16_Q1").style.display = 'none';
				document.getElementById("_Q0_Q16_Q1").value = "";
	}
}

var b = document.getElementsByTagName("Body");
	b[0].onload = function() {
		showpage();
		subtotal();
		ChangeQ2();
	}

if(document.getElementById("_Q0_Q0_Q0_C")) {
	var c = document.getElementById("_Q0_Q0_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q0_Q0_C")) {
	var c = document.getElementById("_Q0_Q0_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q0_Q0_C")) {
	var c = document.getElementById("_Q0_Q0_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q0_Q0_C")) {
	var c = document.getElementById("_Q0_Q0_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q1_Q0_C")) {
	var c = document.getElementById("_Q0_Q1_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q2_Q0_C")) {
	var c = document.getElementById("_Q0_Q2_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q3_Q0_C")) {
	var c = document.getElementById("_Q0_Q3_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q4_Q0_C")) {
	var c = document.getElementById("_Q0_Q4_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q5_Q0_C")) {
	var c = document.getElementById("_Q0_Q5_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q6_Q0_C")) {
	var c = document.getElementById("_Q0_Q6_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q7_Q0_C")) {
	var c = document.getElementById("_Q0_Q7_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q8_Q0_C")) {
	var c = document.getElementById("_Q0_Q8_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q9_Q0_C")) {
	var c = document.getElementById("_Q0_Q9_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q10_Q0_C")) {
	var c = document.getElementById("_Q0_Q10_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q11_Q0_C")) {
	var c = document.getElementById("_Q0_Q11_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q12_Q0_C")) {
	var c = document.getElementById("_Q0_Q12_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q13_Q0_C")) {
	var c = document.getElementById("_Q0_Q13_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q14_Q0_C")) {
	var c = document.getElementById("_Q0_Q14_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q15_Q0_C")) {
	var c = document.getElementById("_Q0_Q15_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};

if(document.getElementById("_Q0_Q16_Q0_C")) {
	var c = document.getElementById("_Q0_Q16_Q0_C");
	//alert(c);
	c.onchange = function() {ChangeQ2();}
};


</script>

I guess Moz simply doesn't fire body.onload .

Try changing:

var b = document.getElementsByTagName("Body");
b[0].onload = function() {
[INDENT]showpage();
subtotal();
ChangeQ2();[/INDENT]
}

to

onload = function(){
[INDENT]showpage();
subtotal();
ChangeQ2();[/INDENT]
}

onload is shorthand for window.onload You can also considerably compact your code by using a loop, eg.:

function ChangeQ2() {
	for(var i=0; i<=16; i++){
		var prefix = "_Q0_Q" + i + "_Q";
		if (document.getElementById(prefix + "0_C").value == "__Other") {
			document.getElementById(prefix + "1").style.display = 'inline';
		} else {
			document.getElementById(prefix + "1").style.display = 'none';
			document.getElementById(prefix + "1").value = "";
		}
	}
}

Do similar for the other large block, which you could also make into a function eg. attachEventsQ2(){.......} called from your anonymous onload function (as above). Thus, all your javascript will be in document's <head></head> where it should be.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.