/    Sign up×
Community /Pin to ProfileBookmark

javascript quiz on the same page

Hi

i am trying to develop javascript quiz like in w3schools.com
i want do do it in the same page.
how can i loop over the arrays to go to next question?
can anyone help?

this is my code

[code=html]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head>
<title></title>
<!–<link rel=”stylesheet” type=”text/css” href=”style.css” />–>

<script type=”text/javascript”>

//define an Array object of questions.
var questionsArray = new Array(10);
questionsArray[0]=”1. Inside which HTML element do we put the JavaScript?”;
questionsArray[1]=”2. What is the correct JavaScript syntax to write “Hello World”?”;
questionsArray[2]=”3. Where is the correct place to insert a JavaScript?”;
questionsArray[3]=”4. What is the correct syntax for referring to an external script called “xxx.js”?”;
questionsArray[4]=”5. How does a “for” loop start?”;
questionsArray[5]=”6. How do you write “Hello World” in an alert box?”;
questionsArray[6]=”7. How do you create a function?”;
questionsArray[7]=”8. How do you call a function named “myFunction”?”;
questionsArray[8]=”9. How do you write a conditional statement for executing some statements only if “i” is equal to 5?”;
questionsArray[9]=”10. How do you write a conditional statement for executing some statements only if “i” is NOT equal to 5?”;

//define an Array object of answers.
var answersArray=new Array(

//answers group 1
“<scripting>”,”<script>”,
“<javascript>”,”<js>”,

//answers group 2
“response.write(“Hello World”)”,””Hello World””,
“(“Hello World”)”,”document.write(“Hello World”)”,

//answers group 3
“The <body> section”,”The <head> section”,
“Both the <head> section and the <body> section are correct”,”The <title> section”,

//answers group 4
“<script name=”xxx.js”>”,”<script src=”xxx.js”>”,
“<script href=”xxx.js”>”,”<script link=”xxx.js”>”,

//answers group 5
“for (i = 0; i <= 5; i++)”,”for (i <= 5; i++)”,
“for i = 1 to 5″,”for (i = 0; i <= 5)”,

//answers group 6
“msgBox(“Hello World”)”,”alertBox=”Hello World””,
“alert(“Hello World”)”,”alertBox(“Hello World”)”,

//answers group 7
“function=myFunction()”,”function myFunction()”,
“function:myFunction()”,”function( myFunction() )”,

//answers group 8
“call myFunction()”,”myFunction()”,
“call function myFunction”,”call myFunction”,

//answers group 9
“if i==5 then”,”if (i==5)”,
“if i=5 then”,”if i=5″,

//answers group 10
“if <> 5″,”if (i != 5)”,
“if (i <> 5)”,”if =! 5 then”

);

//define an Array object of solutions.
var solutionsArray=new Array(“2″,”3″,”2″,”3″,”2″,”3″,”2″,”3″,”2″,”1″);
</script>
</head>

<body onload=”putAns()”>

<div class=”wrapper”>
<div class=”question”>
<p>

<script type=”text/javascript”>

document.write(questionsArray[0]);

</script>

<script type=”text/javascript”>
function putAns(){

for (i=1;i<=4;i++)
{
document.getElementById(“option”+i).value = answersArray[i-1];

}

}
function checkAnswer()
{
var radioOption = document.getElementById(“radOption”).value;
putAns();
}
</script>

</p>
</div>
<form name=”frm” action=”task1.html” method=”get” target=”_top”>
<div class=”answer1″><input name=”radOption” type=”radio” value=”1″ id=”radOption” />
<input id=”option1″ name=”option1″ class=”option” value=”” readonly=”readonly” type=”text” />

</div>
<br />

<div class=”answer2″><input name=”radOption” type=”radio” value=”2″ id=”radOption” />
<input id=”option2″ name=”option2″ class=”option” value=”” readonly=”readonly” type=”text” />
</div>
<br />

<div class=”answer3″><input name=”radOption” type=”radio” value=”3″ id=”radOption” />
<input id=”option3″ name=”option3″ class=”option” value=”” readonly=”readonly” type=”text” />
</div>
<br />

<div class=”answer4″><input name=”radOption” type=”radio” value=”4″ id=”radOption” />
<input id=”option4″ name=”option4″ class=”option” value=”” readonly=”readonly” type=”text” />

</div>
<br />

<div class=”next”>
<input type=”image” onclick=”checkAnswer()”
src=”http://gin.univ-mrs.fr/GINsim/doc/2.2/html/images/next.png” alt=”Next” />
</div>
</form>

</div>
<div class=”footer”>
<p>
<a title=”Check the validity of this site’s XHTML” href=”http://validator.w3.org/check/referer”><img
src=”http://www.w3.org/Icons/valid-xhtml10″
alt=”Valid XHTML 1.0 Transitional” height=”31″ width=”88″ border=”0″ /></a></p>
</div>
</body>
</html>
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERAug 09.2008 — Your 'radOption' elements need to have a unique ID value.

So part of your problem is here:
[code=php]
var radioOption = document.getElementById("radOption").value;
[/code]

It is an invalid method to check the status of a radio button.

There is an options array to check for the selected value.

If you need an alternative check, post back.
Copy linkTweet thisAlerts:
@JMRKERAug 09.2008 — While your original script has a bunch of errors,

and it is not the way I would approach the problem,

here is a start at what you want.

Study the differences ...
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
<title></title>
<!--
<link rel="stylesheet" type="text/css" href="style.css" />
Information above not provided, so substituted below
-->
<style type="text/css">
.question {
width:600px;
}
.answer {
width:600px;
}
.option {
width:400px;
}
</style>

<script type="text/javascript">
// From: http://www.webdeveloper.com/forum/showthread.php?t=188549

//define an Array object of questions.
var questionsArray = [
"1. Inside which HTML element do we put the JavaScript?",
"2. What is the correct JavaScript syntax to write "Hello World"?",
"3. Where is the correct place to insert a JavaScript?",
"4. What is the correct syntax for referring to an external script called "xxx.js"?",
"5. How does a "for" loop start?",
"6. How do you write "Hello World" in an alert box?",
"7. How do you create a function?",
"8. How do you call a function named "myFunction"?",
"9. How do you write a conditional statement for executing some statements only if "i" is equal to 5?",
"10. How do you write a conditional statement for executing some statements only if "i" is NOT equal to 5?"];

//define an Array object of answers.
var answersArray=new Array(

//answers group 1
"<scripting>",
"<script>",
"<javascript>",
"<js>",

//answers group 2
"response.write("Hello World")",
""Hello World"",
"("Hello World")",
"document.write("Hello World")",

//answers group 3
"The <body> section",
"The <head> section",
"Both the <head> section and the <body> section are correct",
"The <title> section",

//answers group 4
"<script name="xxx.js">","<script src="xxx.js">",
"<script href="xxx.js">","<script link="xxx.js">",

//answers group 5
"for (i = 0; i <= 5; i++)",
"for (i <= 5; i++)",
"for i = 1 to 5",
"for (i = 0; i <= 5)",

//answers group 6
"msgBox("Hello World")",
"alertBox="Hello World"",
"alert("Hello World")",
"alertBox("Hello World")",

//answers group 7
"function=myFunction()",
"function myFunction()",
"function:myFunction()",
"function( myFunction() )",

//answers group 8
"call myFunction()",
"myFunction()",
"call function myFunction",
"call myFunction",

//answers group 9
"if i==5 then",
"if (i==5)",
"if i=5 then",
"if i=5",

//answers group 10
"if <> 5",
"if (i != 5)",
"if (i <> 5)",
"if =! 5 then"

);


//define an Array object of solutions.
var solutionsArray=new Array("2","3","2","3","2","3","2","3","2","1");
var responsesArray=new Array('','','','','','','','','','');

function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) {
str = sel[i].value; sel[i].checked = false; fnd = i;
}
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above

return str;
}

var QNo = 0;

</script>
</head>

<body onload="checkAnswer()">

<div class="wrapper">
<div class="question" id="question"></div>
<p>

<script type="text/javascript">

function putAns(){
document.getElementById('question').innerHTML = questionsArray[QNo];
for (i=1;i<=4;i++) {
document.getElementById("option"+i).value = answersArray[(QNo*4)+(i-1)];
}
}
function checkAnswer() {
// need to save responses
if (QNo <10) {
responsesArray[QNo] = getRBtnName('radOption');
putAns();
QNo++;
} else {
var str = 'Your responses:tCorrect responses:n';
for (var i=0; i<10; i++) { str += responsesArray[i]+'t'+solutionsArray[i]+'n'; }
alert(str);
}
}
</script>

</p>

<form name="frm" action="" method="get" target="_top" onsubmit="return false">
<div class="answer">
<input name="radOption" type="radio" value="1" />
<input id="option1" name="option1" class="option" value="" readonly="readonly" type="text" />
</div>
<br />
<div class="answer">
<input name="radOption" type="radio" value="2" />
<input id="option2" name="option2" class="option" value="" readonly="readonly" type="text" />
</div>
<br />
<div class="answer">
<input name="radOption" type="radio" value="3" />
<input id="option3" name="option3" class="option" value="" readonly="readonly" type="text" />

</div>
<br />
<div class="answer">
<input name="radOption" type="radio" value="4" />
<input id="option4" name="option4" class="option" value="" readonly="readonly" type="text" />

</div>
<br />
<div class="next">
<input type="image" onclick="checkAnswer()"
src="http://gin.univ-mrs.fr/GINsim/doc/2.2/html/images/next.png" alt="Next" />
</div>

</form>

</div>

<div class="footer">
<p>
<a title="Check the validity of this site’s XHTML" href="http://validator.w3.org/check/referer">
<img src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0 Transitional" height="31" width="88" border="0" />
</a>
</p>
</div>
</body>
</html>
[/code]
×

Success!

Help @klaibert26 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...