I am trying to make a simple quiz app in vanilla javascript. When a user clicks on the wrong option, I want to change the background color to red and the color of correct answer to lightGreen. After these two color changes, I don’t want to change the background color of two remaining options to red when a user clicks on them.
Similarly, when a user clicks on the right answer, I want to change the background color of correct answer to lightGreen and don’t want other options to change their background color to red when a user clicks on them(after clicking the correct answer). Here is the app image. I am using “ul” and “li” for heading and options(not using button for option)I tried to make it work out but failed to do so. I am attaching the quiz app image and the code:
<!-- Html Code -->
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="./styles.css" />
</head>
<body>
<!-- Quiz App -->
<div class="quiz-app-background">
<div class="quiz-app-container">
<div class="simple-quiz-heading">
<h1>Simple Quiz</h1>
</div>
<div class="simple-quiz-line">
<hr />
</div>
<div id="quiz-questions-container" class="quiz-questions-container">
<div class="first-question">
<ul>
<h3 class="first-heading">
1. Which is the largest animal in the world ?
</h3>
</ul>
<li
class="first-question-first-option first-question-first-option-color-on-removing-mouse"
>
Shark
</li>
<li class="first-question-second-option">Blue Whale</li>
<li class="first-question-third-option">Elephant</li>
<li class="first-question-fourth-option">Giraffe</li>
<div class="next-button-div-1">
<button class="next-button-1">Next</button>
</div>
</div>
<div class="second-question">
<ul>
<h3 class="second-heading">
2. Which is the smallest country in the world ?
</h3>
</ul>
<li class="second-question-first-option">Vatican City</li>
<li class="second-question-second-option">Bhutan</li>
<li class="second-question-third-option">Nepal</li>
<li class="second-question-fourth-option">Sri Lanka</li>
<div class="next-button-div-2">
<button class="next-button-2">Next</button>
</div>
</div>
<div class="third-question">
<ul>
<h3 class="third-heading">
3. Which is the largest desert in the world ?
</h3>
</ul>
<li class="third-question-first-option">Kalahari</li>
<li class="third-question-second-option">Gobi</li>
<li class="third-question-third-option">Sahara</li>
<li class="third-question-fourth-option">Antarctica</li>
<div class="next-button-div-3">
<button class="next-button-3">Next</button>
</div>
</div>
<div class="fourth-question">
<ul>
<h3>4. Which is the smallest continent in the world ?</h3>
</ul>
<li class="fourth-question-first-option">Asia</li>
<li class="fourth-question-second-option">Australia</li>
<li class="fourth-question-third-option">Arctic</li>
<li class="fourth-question-fourth-option">Africa</li>
<div class="next-button-div-4">
<button class="next-button-4">Next</button>
</div>
</div>
</div>
<div class="result-container">
<div class="result-text">
<h3 class="result-heading"></h3>
</div>
<div class="result-btn">
<button class="play-gain-btn">Play Again</button>
</div>
</div>
</div>
</div>
</div>
<script src="./script.js"></script>
</body>
</html>
/* css code */
.quiz-app-background{
background-color: rgb(2, 2, 69);
color: white;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.quiz-app-container {
background-color: white;
color: black;
border-radius: 10px;
width: 600px;
height: 380px;
}
.simple-quiz-heading {
margin: 0px 10%;
}
.simple-quiz-line {
margin: 0px 10%;
}
.first-question ul li {
list-style-type: none;
}
.first-question-first-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.first-question-second-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.first-question-third-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.first-question-fourth-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.third-heading {
display: none;
}
.fourth-heading {
display: none;
}
.next-button-div-1 {
display: flex;
justify-content: center;
}
.next-button-div-2 {
display: flex;
justify-content: center;
}
.next-button-1 {
display: flex;
justify-content: center;
color: white;
background-color: #020245;
border: none;
border-radius: 6px;
padding: 10px 35px;
margin-top: 5%;
cursor: pointer;
}
.next-button-1 {
display: none;
}
.next-button-2 {
display: none;
color: white;
background-color: #020245;
border: none;
border-radius: 6px;
padding: 10px 35px;
margin-top: 5%;
cursor: pointer;
}
.second-question-first-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.second-question-second-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.second-question-third-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.second-question-fourth-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.second-question {
display: none;
}
.third-question {
display: none;
}
.third-question-first-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.third-question-second-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.third-question-third-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.third-question-fourth-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.next-button-3 {
display: none;
color: white;
background-color: #020245;
border: none;
border-radius: 6px;
padding: 10px 35px;
margin-top: 5%;
cursor: pointer;
}
.next-button-div-3 {
display: flex;
justify-content: center;
}
.fourth-question {
display: none;
}
.fourth-question-first-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.fourth-question-second-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.fourth-question-third-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.fourth-question-fourth-option {
list-style-type: none;
margin-left: 10%;
border: 1px solid black;
margin-right: 60px;
border-radius: 5px;
margin-bottom: 2%;
padding: 5px;
cursor: pointer;
}
.next-button-div-4 {
display: flex;
justify-content: center;
}
.next-button-4 {
display: none;
color: white;
background-color: #020245;
border: none;
border-radius: 6px;
padding: 10px 35px;
margin-top: 5%;
cursor: pointer;
}
.result-container {
display: none;
}
.result-text {
display: flex;
margin-left: 10%;
}
.result-btn {
display: flex;
justify-content: center;
}
.play-gain-btn {
display: flex;
justify-content: center;
color: white;
background-color: #020245;
border: none;
border-radius: 6px;
padding: 10px 35px;
margin-top: 5%;
cursor: pointer;
}
// Javascript code
// Accessing the first option
let firstOption = document.querySelector(".first-question-first-option");
// Accessing the second option
let secondOption = document.querySelector(".first-question-second-option");
// Accessing the third option
let thirdOption = document.querySelector(".first-question-third-option");
// Accessing the fourth option
let fourthOption = document.querySelector(".first-question-fourth-option");
let nextButtonOne = document.querySelector(".next-button-1");
let nextButtonTwo = document.querySelector(".next-button-2");
let nextButtonThree = document.querySelector(".next-button-3");
let nextButtonFour = document.querySelector(".next-button-4");
let firstQuestion = document.querySelector(".first-question");
let secondQuestion = document.querySelector(".second-question");
let thirdQuestion = document.querySelector(".third-question");
let fourthQuestion = document.querySelector(".fourth-question");
let secondQuestionFirstOption = document.querySelector(
".second-question-first-option"
);
let secondQuestionSecondOption = document.querySelector(
".second-question-second-option"
);
let secondQuestionThirdOption = document.querySelector(
".second-question-third-option"
);
let secondQuestionFourthOption = document.querySelector(
".second-question-fourth-option"
);
let thirdHeading = document.querySelector(".third-heading");
let thirdQuestionFirstOption = document.querySelector(
".third-question-first-option"
);
let thirdQuestionSecondOption = document.querySelector(
".third-question-second-option"
);
let thirdQuestionThirdOption = document.querySelector(
".third-question-third-option"
);
let thirdQuestionFourthOption = document.querySelector(
".third-question-fourth-option"
);
let fourthQuestionFirstOption = document.querySelector(
".fourth-question-first-option"
);
let fourthQuestionSecondOption = document.querySelector(
".fourth-question-second-option"
);
let fourthQuestionThirdOption = document.querySelector(
".fourth-question-third-option"
);
let fourthQuestionFourthOption = document.querySelector(
".fourth-question-fourth-option"
);
let resultContainer = document.querySelector(".result-container");
let resultBtn = document.querySelector(".result-btn");
let resultHeading = document.querySelector(".result-heading");
// Adding padding to the options
firstOption.style.padding = "5px";
secondOption.style.padding = "5px";
thirdOption.style.padding = "5px";
fourthOption.style.padding = "5px";
firstOption.style.color = "black";
secondOption.style.color = "black";
thirdOption.style.color = "black";
fourthOption.style.color = "black";
firstOption.style.cursor = "pointer";
secondOption.style.cursor = "pointer";
thirdOption.style.cursor = "pointer";
fourthOption.style.cursor = "pointer";
secondQuestionFirstOption.style.padding = "5px";
secondQuestionFirstOption.style.cursor = "pointer";
secondQuestionFirstOption.style.backgroundColor = "transparent";
secondQuestionSecondOption.style.padding = "5px";
secondQuestionThirdOption.style.padding = "5px";
secondQuestionThirdOption.style.cursor = "pointer";
secondQuestionFourthOption.style.padding = "5px";
secondQuestionFourthOption.style.cursor = "pointer";
function firstOption1() {
firstOption.style.backgroundColor = "red";
firstOption.style.color = "white";
secondOption.style.backgroundColor = "lightGreen";
secondOption.style.color = "white";
nextButtonOne.style.display = "block";
}
firstOption.addEventListener("click", firstOption1);
function secondOption1() {
secondOption.style.backgroundColor = "lightGreen";
secondOption.style.color = "white";
nextButtonOne.style.display = "block";
}
secondOption.addEventListener("click", secondOption1);
function thirdOption1() {
thirdOption.style.backgroundColor = "red";
thirdOption.style.color = "white";
secondOption.style.backgroundColor = "lightGreen";
secondOption.style.color = "white";
nextButtonOne.style.display = "block";
}
function thirdOption2() {
thirdOption.style.backgroundColor = "white";
thirdOption.style.color = "black";
}
thirdOption.addEventListener("click", thirdOption1);
if (
(firstOption.style.backgroundColor == "red" &&
firstOption.style.color == "white") ||
(secondOption.style.backgroundColor == "lightGreen" &&
secondOption.style.color == "white")
) {
thirdOption.removeEventListener("click", thirdOption1);
thirdOption.addEventListener("click", thirdOption2);
}
function fourthOption1() {
fourthOption.style.backgroundColor = "red";
fourthOption.style.color = "white";
secondOption.style.backgroundColor = "lightGreen";
secondOption.style.color = "white";
nextButtonOne.style.display = "block";
}
fourthOption.addEventListener("click", fourthOption1);
secondQuestionFirstOption.addEventListener("click", function () {
secondQuestionFirstOption.style.backgroundColor = "lightGreen";
secondQuestionFirstOption.style.color = "white";
nextButtonTwo.style.display = "block";
});
secondQuestionSecondOption.addEventListener("click", function () {
secondQuestionSecondOption.style.backgroundColor = "red";
secondQuestionFirstOption.style.color = "white";
secondQuestionFirstOption.style.backgroundColor = "lightGreen";
secondQuestionSecondOption.style.color = "white";
nextButtonTwo.style.display = "block";
});
secondQuestionThirdOption.addEventListener("click", function () {
secondQuestionThirdOption.style.backgroundColor = "red";
secondQuestionThirdOption.style.color = "white";
secondQuestionFirstOption.style.backgroundColor = "lightGreen";
secondQuestionFirstOption.style.color = "white";
secondQuestionThirdOption.style.color = "white";
nextButtonTwo.style.display = "block";
});
secondQuestionFourthOption.addEventListener("click", function () {
secondQuestionFourthOption.style.backgroundColor = "red";
secondQuestionFirstOption.style.backgroundColor = "lightGreen";
secondQuestionFirstOption.style.color = "white";
secondQuestionFourthOption.style.color = "white";
nextButtonTwo.style.display = "block";
});
thirdQuestionFirstOption.addEventListener("click", function () {
thirdQuestionFirstOption.style.backgroundColor = "red";
thirdQuestionFourthOption.style.backgroundColor = "lightGreen";
thirdQuestionFourthOption.style.color = "white";
thirdQuestionFirstOption.style.color = "white";
nextButtonThree.style.display = "block";
});
thirdQuestionSecondOption.addEventListener("click", function () {
thirdQuestionSecondOption.style.backgroundColor = "red";
thirdQuestionFourthOption.style.backgroundColor = "lightGreen";
thirdQuestionFourthOption.style.color = "white";
thirdQuestionSecondOption.style.color = "white";
nextButtonThree.style.display = "block";
});
thirdQuestionThirdOption.addEventListener("click", function () {
thirdQuestionThirdOption.style.backgroundColor = "red";
thirdQuestionFourthOption.style.backgroundColor = "lightgreen";
thirdQuestionFourthOption.style.color = "white";
thirdQuestionThirdOption.style.color = "white";
nextButtonThree.style.display = "block";
});
thirdQuestionFourthOption.addEventListener("click", function () {
thirdQuestionFourthOption.style.backgroundColor = "lightGreen";
thirdQuestionFourthOption.style.color = "white";
nextButtonThree.style.display = "block";
});
fourthQuestionFirstOption.addEventListener("click", function () {
fourthQuestionFirstOption.style.backgroundColor = "red";
fourthQuestionFirstOption.style.color = "white";
fourthQuestionSecondOption.style.backgroundColor = "lightGreen";
fourthQuestionSecondOption.style.color = "white";
nextButtonFour.style.display = "block";
});
fourthQuestionSecondOption.addEventListener("click", function () {
fourthQuestionSecondOption.style.backgroundColor = "lightGreen";
fourthQuestionSecondOption.style.color = "white";
nextButtonFour.style.display = "block";
});
fourthQuestionThirdOption.addEventListener("click", function () {
fourthQuestionThirdOption.style.backgroundColor = "red";
fourthQuestionThirdOption.style.color = "white";
fourthQuestionSecondOption.style.backgroundColor = "lightGreen";
fourthQuestionSecondOption.style.color = "white";
nextButtonFour.style.display = "block";
});
fourthQuestionFourthOption.addEventListener("click", function () {
fourthQuestionFourthOption.style.backgroundColor = "red";
fourthQuestionFourthOption.style.color = "white";
fourthQuestionSecondOption.style.backgroundColor = "lightGreen";
fourthQuestionSecondOption.style.color = "white";
nextButtonFour.style.display = "block";
});
nextButtonOne.addEventListener("click", function () {
firstQuestion.style.display = "none";
secondQuestion.style.display = "block";
secondQuestionFirstOption.style.backgroundColor = "transparent";
secondQuestionFirstOption.style.color = "black";
});
nextButtonTwo.addEventListener("click", function () {
secondQuestion.style.display = "none";
thirdQuestion.style.display = "block";
thirdHeading.style.display = "block";
});
nextButtonThree.addEventListener("click", function () {
thirdQuestion.style.display = "none";
fourthQuestion.style.display = "block";
});
nextButtonFour.addEventListener("click", function () {
fourthQuestion.style.display = "none";
resultContainer.style.display = "block";
resultHeading.textContent = `You scored out of 4`;
});
resultBtn.addEventListener("click", function () {
resultContainer.style.display = "none";
setTimeout(() => {
firstQuestion.style.display = "block";
}, 1000);
window.location.reload();
});