Show sourcecode
The following files exists in this folder. Click to view.
quiz.js
55 lines ASCII Windows (CRLF)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
const questions = document.querySelectorAll(".question");
const prevQuestionBtn = document.querySelector("#prev_question");
const nextQuestionBtn = document.querySelector("#next_question");
const submitBtn = document.querySelector("#submit_quiz");
let questionIndex = 0;
nextQuestionBtn.addEventListener("click", () => {
if (questionIndex == 0) {
prevQuestionBtn.style.display = "flex";
}
if (questionIndex < questions.length - 1) {
questions[questionIndex].style.display = "none";
questionIndex++;
questions[questionIndex].style.display = "flex";
}
if (questionIndex == questions.length - 1) {
nextQuestionBtn.style.display = "none";
submitBtn.style.display = "flex";
}
});
prevQuestionBtn.addEventListener("click", () => {
nextQuestionBtn.style.display = "flex";
submitBtn.style.display = "none";
if (questionIndex > 0) {
questions[questionIndex].style.display = "none";
questionIndex--;
questions[questionIndex].style.display = "flex";
}
if (questionIndex == 0) {
prevQuestionBtn.style.display = "none";
}
});
prevQuestionBtn.style.display = "none";
if (questions.length == 1) {
nextQuestionBtn.style.display = "none";
submitBtn.style.display = "flex";
} else {
nextQuestionBtn.style.display = "flex";
submitBtn.style.display = "none";
}
for (let i = 0; i < questions.length; i++) {
if (i == 0) {
questions[i].style.display = "flex";
} else {
questions[i].style.display = "none";
}
}