I am learning javascript and writing a Quiz program as part of learning process.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form type="submit" onsubmit = "populateQuestion()">
<h2 id="question">Question goes here</h2>
<input name = "option" type="radio" value="A"><span id = "choiceA">First option here</span><br/>
<input name = "option" type="radio" value="B"><span id = "choiceB">Second option here</span><br/>
<input name = "option" type="radio" value="C"><span id = "choiceC">Third option here</span><br/>
<input name = "option" type="radio" value="D"><span id = "choiceD">Fourth option here</span><br/>
<button type = "submit">Submit</button>
</form>
</body>
<script>
var question = document.getElementById("question");
var choiceA = document.getElementById("choiceA");
var choiceB = document.getElementById("choiceB");
var choiceC = document.getElementById("choiceC");
var choiceD = document.getElementById("choiceD");
var quesIndex = 0;
var defQuestions = [
{
question : "Q01?",
choiceA : "A1",
choiceB : "B1",
choiceC : "C1",
choiceD : "D1",
answer : "A"
},
{
question : "Q02?",
choiceA : "A2",
choiceB : "B2",
choiceC : "C2",
choiceD : "D2",
answer : "A"
},
{
question : "Q03?",
choiceA : "A3",
choiceB : "B3",
choiceC : "C3",
choiceD : "D3",
answer : "B"
},
{
question : "Q04?",
choiceA : "A4",
choiceB : "B4",
choiceC : "C4",
choiceD : "D4",
answer : "B"
}
];
function renderQuestion() {
question.innerHTML = defQuestions[quesIndex].question;
choiceA.innerHTML = defQuestions[quesIndex].choiceA;
choiceB.innerHTML = defQuestions[quesIndex].choiceB;
choiceC.innerHTML = defQuestions[quesIndex].choiceC;
choiceD.innerHTML = defQuestions[quesIndex].choiceD;
}
function populateQuestion() {
console.log(quesIndex);
renderQuestion();
quesIndex += 1;
}
populateQuestion();
</script>
</html>
When I load the page, populateQuestion() executed and loaded the default question and options (the first element of defQuestions array). This part is working fine.
When I click on Submit button, the next question supposed to be loaded. But the next question is not loading.
The console.log(quesIndex); prints only 0 and then the console window logs clearing.
Is there anything wrong in the implementation?
