Hi I have a javascript quiz (thanks to Pullo’s guidance), and
now I´d like to change to radio buttons with around 3 or 4 options, like for example:
Join the 2 sentences:
Mary is English. She was born in London
·Radio buttons: “A”. Mary was born in England
“B” Mary, who is English, was born in London
“C” English Mary was born in London
“D” London Mary is English born
So, I was thinking of around 10 questions in the quiz , with a submit button at the bottom, and a ·“tick” after correct answers, and “x” after incorrect answers.
Or, maybe to put it another way, how could I modify the following code, replacing the dropdown menus with radio buttons?
<form action="#" method="post" id="myForm" name="f">
<ol>
<li>
I think blue is the colour that
<select name="question1">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
I like best.
</li>
<li>
Simon, who
<select name="question2">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
is 25, got married last week.
</li>
<li>
My car has a mechanical problem that / which
<select name="question3">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
is expensive to repair.
</li>
<li>
He left the keys that
<select name="question4">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
I gave him on the table.
</li>
<li>
The carpenter who / that
<select name="question5">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
made my furniture is very rich.
</li>
<li>
He took a plane that
<select name="question6">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
left from London.
</li>
<li>
The money that / which
<select name="question7">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
you borrowed was not really necessary.
</li>
<li>
James is the most honest person that
<select name="question8">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
I've ever met.
</li>
<li>
They're the people who /that
<select name="question9">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
live next door.
</li>
<li>
The idea, which
<select name="question10">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
was very original, has been accepted.
</li>
<li>
The book that / which
<select name="question11">
<option value="na"> </option>
<option value="a"> optional </option>
<option value="b"> necessary</option>
</select>
I bought is very interesting.
</li>
</ol>
<div id="buttons">
<input type="submit" id="submit" value="Result" />
<input type="reset" class="reset" id="reset" name="reset" value="Start again" />
</form> </div> </div>
<div id="score"></div>
<div class="address">
John Kelly © 2006 - 2013
</div>
</div>
</div>
</div>
</div>
<script>
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
(function(){
"use strict";
window.checkAnswers = function(opts){
function validateInput(){
var question,
answer;
for (question in opts) {
if(opts.hasOwnProperty(question)) {
answer = f.elements[question].options[f.elements[question].selectedIndex].value;
if(answer === "na"){
opts[question].state = "not-filled-in";
} else if(answer === opts[question].answer){
opts[question].state = "correct";
} else {
opts[question].state = "error";
}
}
}
}
function markCorrectOrIncorrect(){
var question,
li;
for (question in opts) {
if(opts.hasOwnProperty(question)) {
var img = new Image(),
li = f.elements[question].parentElement,
feedbackImg = li.getElementsByTagName('img')[0];
if (feedbackImg){
li.removeChild(feedbackImg);
}
if(opts[question].state === "correct"){
img.src = "http://www.littletherese.com/tick.jpg";
li.appendChild(img)
} else if(opts[question].state === "error"){
img.src = "http://www.littletherese.com/x.jpg";
li.appendChild(img)
}
}
}
}
function displayScore(){
var correct = 0,
error = 0,
score = document.getElementById("score"),
totalQuestions = Object.size(opts),
question;
for (question in opts) {
if(opts.hasOwnProperty(question)) {
if(opts[question].state === "correct"){
correct ++
} else if(opts[question].state === "error"){
error ++
}
}
}
score.innerHTML = "You got " + correct + " out of " + totalQuestions;
}
function init(){
validateInput();
markCorrectOrIncorrect();
displayScore();
}
init();
}
}());
f.onsubmit = function(){
checkAnswers({
question1: {answer: "a"},
question2: {answer: "b"},
question3: {answer: "b"},
question4: {answer: "a"},
question5: {answer: "b"},
question6: {answer: "b"},
question7: {answer: "a"},
question8: {answer: "a"},
question9: {answer: "b"},
question10: {answer: "b"},
question11: {answer: "a"},
})
return false;
}
f.reset.onclick = function(){
location.reload();
}
</script>
</body>
</html>
Thanks for any help!