I have a problem with the following code , i put try ... catch around if statement , and display this error message :
'Question1' is undefined !

<html>
	<head> 
		<title> Online Quiz </title>
		<script type="text/JavaScript">
		
			function checkAnswer(){
			
			    var correctAnswers=0;
				var errorAnswers=0;
				document.writeln("The Result : ");
	                  try{			 
				if(Question1.Q1[1].checked){
				document.writeln("Q1 : Great !, True Answer is B !");
				correctAnswers++;
				}else{
				errorAnswers++;
				}
				  }
                                  catch(err)
                                   {
                                     txt="There was an error on this page.\n\n";
                                     txt+="Error description: " + err.description + "\n\n";
                                     txt+="Click OK to continue.\n\n";
                                     alert(txt);
                                    }
				/*
				if(Question2.Q2[2].checked){
				document.writeln("Q2 : Great !, True Answer is C !");
				correctAnswers++;
		         }else{
				errorAnswers++;
				}
				
				if(Question3.Q3[0].checked){
				 document.writeln("Q3 : Great !, True Answer is A !");
				 correctAnswers++;
				 }else{
				errorAnswers++;
				}
				
				if(Question4.Q4[0].checked){
				 document.writeln("G4 : Great !, True Answer is A !");
				 correctAnswers++;
				 }else{
				errorAnswers++;
				}
				
				if(Question5.Q5[0].checked){
				 document.writeln("G5 : Great !, True Answer is A !");
				 correctAnswers++;
				 }else{
				errorAnswers++;
				}
				*/
				 
				 document.write("Your Correct Answers : "+correctAnswers);
				 document.write("Your InCorrect Answers : "+errorAnswers);
			}

		</script>

		<style>
		p{
		font-size:24px;
		font-style:italic;
		color:blue;
		}
		
		</style>

	</head>

	<body>
	<form method="get" id="QForm" >
		<div id="Question1">
		<p>1. What does CSS stand for ?</p>
		<input type="radio" name="Q1" id="hello"/>a. Creative Style Sheets
		<input type="radio" name="Q1" />b. Cascading Style Sheets
		<input type="radio" name="Q1" />c. Computer Style Sheets
		</div>
		<hr/>
		
		<div id="Question2">
		<p>2. Which HTML tag is used to define an embedded styles ?</p>
		<input type="radio" name="Q2" />a. < script >
		<input type="radio" name="Q2" />b. < css >
		<input type="radio" name="Q2" />c. < style >
		</div>
		<hr/>
		
		<div id="Question3">
		<p>3. Which HTML attribute is used to define inline styles ?</p>
		<input type="radio" name="Q3" />a. style
		<input type="radio" name="Q3" />b. class
		<input type="radio" name="Q3" />c. styles
		</div>
		<hr/>
		
		<div id="Question4">
		<p>4. Which is the correct CSS syntax ?</p>
		<input type="radio" name="Q4" />a. body {color: black}
		<input type="radio" name="Q4" />b. {body:color=black(body}
		<input type="radio" name="Q4" />c. {body;color:black}
		</div>
		<hr/>
		
		<div id="Question5">
		<p>5. Which property is used to change the background color ?</p>
		<input type="radio" name="Q5" />a. background-color:
		<input type="radio" name="Q5" />b. color:
		<input type="radio" name="Q5" />c. bgcolor:
		</div>
		<hr/>
		
		<div>
		<input type="submit" name="subbutton" value="submit All" onclick="checkAnswer()" />
		<input type="reset" name="rebutton" value="reset All"/>
		</div>
		
		
		</form>
	</body>
</html>

i don't know what is the problem , please Help !

Recommended Answers

All 6 Replies

Member Avatar for stbuchok

Use document.getElementById();

commented: Great post stB! +13
Member Avatar for stbuchok

Gee, thanks for down voting a suggestion, I don't even really want to help you, but I just can't help but point out your mistake now. When you use document.writeln you are clearing all the content on the page, therefore the control doesn't exist.

Next time just say, no that doesn't work rather than down voting, otherwise next time you're on your own.

Gee, thanks for down voting a suggestion, I don't even really want to help you, but I just can't help but point out your mistake now. When you use document.writeln you are clearing all the content on the page, therefore the control doesn't exist.

Next time just say, no that doesn't work rather than down voting, otherwise next time you're on your own.

ooh I'm so sorry , but I'm new here and i thought that if the solution doesn't work just click on down arrow !

I'm so sorry
but when i used getelementbyid , the page just loaded and clear checked radio buttons without any result !

Member Avatar for stbuchok

lol, I forgive you.

Do you understand why it is not working now. Also it is good practice if you aren't using jQuery to have your javascript load at the bottom of the page, that way all the elements on the page are loaded.

ooh I'm so sorry , but I'm new here and i thought that if the solution doesn't work just click on down arrow !

What a giggle :icon_cheesygrin:

I appreciate the misunderstanding.

Glad to see we're all friends again :icon_cool:

Airshow

Member Avatar for stbuchok

;)

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.