I am making a quiz where the text (questions, answers etc.) will be loaded with javaScript.
So far I have created two buttons, (one going forward, the other backwards); And I'd like the buttons to load up my questions when clicked...
This is the array:
var allQuestions = [{
"question": "Who was Luke's wingman in the battle at Hoth?",
"choices": ["Dak", "Biggs", "Wedge", "fx-7"],
"correctAnswer": 0
}, {//question #2 }, {//question #3 etc},...];
This is my Js:
var button = document.querySelector('.navForward');
button.addEventListener('click', function() {
var questionOutput = " ";
var currentQuestion = 0;
var item = allQuestions;
if(currentQuestion < item.length){
currentQuestion++;
}
questionOutput += "<h4>" + item.question[currentQuestion] + "</h4> <br/>";
var update = document.getElementById("question");
update.innerHTML = questionOutput;
}, false);
And could someone explain this error I get in the console?
Uncaught TypeError: Cannot read property '1' of undefined script.js:84 (anonymous function)
There is no question
property in the item
object, as it's an array.
Change item.question[currentQuestion]
to item[currentQuestion].question
.
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.