Hi all,
I am just beginning to learn javascript…so to this point i no very little….I have a multiplechoice quiz with more than one possible answer for some of the questions when i click submit button it tells me my results by way of an alert box….what i want to happen is when submit button is clicked the questions will stay on the page and the correct answers will be highlighted in any color and it’ll show me if i got them right or wrong….If this is possible any help would be greatly appreciated on how to do it…here is the code for the quiz to date…
[CODE]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>Untitled</title>
<script language=”JavaScript” type=”text/javascript”>
<!–
var questions=new Array();
questions[0]=new Array(1,0,1,0);//first and third answer=correct, others=incorrect
questions[1]=new Array(0,0,0,1);//fourth answer=correct, others=incorrect
questions[2]=new Array(0,0,1,0);//third answer=correct, others=incorrect
function checkAnswers(){
var falseAnswers=new Array();
var answers=new Array();
for(var i=0;i<questions.length;i++){
answers[i]=new Array();
}
var boxArr=new Array();
var checkArrays=new Array();
var count=-1;
var cbox=document.getElementsByTagName(‘input’);
for (var i=0;i<cbox.length;i++){
if(cbox[i].type==’checkbox’){
count++;
boxArr[count]=cbox[i].name;
}
}
for(var i=0;i<boxArr.length;i++){
checkArrays[i]=boxArr[i].split(‘_’);
var quest=checkArrays[i][1];
for(var j=0;j<questions.length;j++){
if(quest==j){
if(document.myquiz[‘question_’+j+’_’+checkArrays[i][2]].checked){
var ins=1;
}
else{
var ins=0;
}
answers[j][checkArrays[i][2]]=(ins);
}
}
}
count=-1;
for(var j=0;j<questions.length;j++){
var answ=answers[j].toString();
var ques=questions[j].toString() ;
if(ques!=answ){
count++;
falseAnswers[count]=’Mistake in question ‘+(j+1)+’n’;
}
}
var mistakes=””;
for(var j=0;j<falseAnswers.length;j++){
mistakes+=falseAnswers[j];
}
if (mistakes==””){
mistakes=”Congratulations! All questions answered correctly!”;
}
alert(mistakes);
}
//–>
</script>
</head>
<body>
<p align=”center”>
<form method=”POST” name=”myquiz”>
<div class=”header”>Quiz<br><br></div>
<div class=”qheader”>
1) What is an ant?</div>
<div class=”qselections”>
<input type=”checkbox” value=”a” name=”question_0_0″>a) Animal<br>
<input type=”checkbox” value=”b” name=”question_0_1″>b) Piece of furniture<br>
<input type=”checkbox” value=”c” name=”question_0_2″>c) Insect<br>
<input type=”checkbox” value=”d” name=”question_0_3″>d) Drink<br>
</div>
<br>
<div class=”qheader”>
2) Which of these planets is closest to the sun?</div>
<div class=”qselections”>
<input type=”checkbox” value=”a” name=”question_1_0″>a) Pluto<br>
<input type=”checkbox” value=”b” name=”question_1_1″>b) Jupiter<br>
<input type=”checkbox” value=”c” name=”question_1_2″>c) Saturn<br>
<input type=”checkbox” value=”d” name=”question_1_3″>d) Venus<br>
</div>
<br>
<div class=”qheader”>
3) Which of these animals can fly</div>
<div class=”qselections”>
<input type=”checkbox” value=”a” name=”question_2_0″>a) Rhinoceros<br>
<input type=”checkbox” value=”b” name=”question_2_1″>b) Dog<br>
<input type=”checkbox” value=”c” name=”question_2_2″>c) Butterfly<br>
<input type=”checkbox” value=”d” name=”question_2_3″>d) Gorilla<br>
</div>
<input type=”button” value=”check” onclick=”checkAnswers()”>
</form>
<br>
</html>
Thanks in advance
Rgds
Niall