I am creating a quiz using jQuery. I have established the quiz questions and answers in a 2-dimensional array. I have the answer choices (1-5) displayed in a separate array. I have stored the answer choices to temporary variables and want to display them next to radio buttons. The answer choices (noted in my code as answerA=answerChoices[j][0], etc) are being input correctly but they are not displaying. When I inspect the elements in Chrome, I see that they are being brought in. However they are not displaying next to the form element in #quizQuestion.
I am sure I am doing something silly but cannot identify it. CAN YOU HELP?
Here's the html:
<!--practice quiz-->
<div id="practiceQuestions">
<div class="questionBox">
<div class="theQuestion">
<span id="quizQuestion"><img src="images/Practice_Tips_Math.png"></span>
<INPUT type="submit" value="Start quiz now" id="startQuiz" class="viewable">
<!--answer key beginsb-->
<div class="answerBox">
<form name="answerForm">
<input type="radio" value="a" id="answerA" name="quizAnswer"><br />
<input type="radio" value="b" id="answerB" name="quizAnswer"><br />
<input type="radio" value="c" id="answerC" name="quizAnswer"><br />
<input type="radio" value="d" id="answerD" name="quizAnswer"><br />
<input type="radio" value="e" id="answerE" name="quizAnswer"><br />
</form></div>
<!--answer key end-->
</div>
</div>
</div>
<!--practice quiz ends-->
And here's the jQuery:
var questionAnswer=[
['How many moons does Earth have?',1],
['How many moons does Saturn have?',31],
['How many moons does Venus have?',0]
];
var answerChoices=[
[1,2,3,4,5],
[18,32,15,1,9],
[3,5,1,0,9]
]
function askQuestion(j){
//create radio button answer choices a-e
//var radioBtn =$('<input type="radio" name="rbtnCount" value="a" id="q1a"/><label for="q1a">'+question[0,1]+'</label><br/>');
//display questions
//console.log(radioBtn);
var answerA=answerChoices[j][0];
var answerB=answerChoices[j][1];
var answerC=answerChoices[j][2];
var answerD=answerChoices[j][3];
var answerE=answerChoices[j][4];
if($('#startQuiz').is(':visible')) {
$('#startQuiz').toggle();
}
if($('.answerBox').is(':hidden')) {
$('.answerBox').toggle();
$('.answerBox').css('margin-top','20px');
}
// display the question
$(".theQuestion").css('margin-top','75px');
$("#quizQuestion").text(questionAnswer[j][0]);
//display the answers
$('form #answerA').text(answerA);
$('form #answerB').text(answerB);
$('form #answerC').text(answerC);
$('form #answerD').text(answerD);
$('form #answerE').text(answerE);
};
$(document).ready(function(){
$('#startQuiz').on('click',function(event){
event.preventDefault();
for (var i=0; i<=questionAnswer.length; i++)
{
askQuestion(i);
}
});
});
You can't set text()
to a radio button, so you have to have some kind of label next to the radio button:
update:
<input type="radio" value="a" id="answerA" name="quizAnswer">
to
<input type="radio" value="a" id="answerA" name="quizAnswer">
<label for="answerA"></label>
and update your jQuery:
$('form #answerA').text(answerA);
to:
$('form label[for="answerA"]').text(answerA);
JSFiddle Example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.