Currently, I am working on creating an exam page that features choices with checkboxes. My goal is to have the background of a selected choice change color. However, I am encountering some obstacles.
To view the codes and screen, please click here
Here is the HTML code snippet:
<div class="questions">
<div class="questions-left-column">
<div class="question-area">
<p>QUESTION</p>
<div class="choices">
<div class="choice answer" id="1answer">
<input id="choice-1a" type="radio" name="radio1" />
<label for="choice-1a">
<strong>A.</strong>
Option A
</label>
</div>
<div class="choice">
<input id="choice-1b" type="radio" name="radio1" />
<label for="choice-1b">
<strong>B.</strong>
Option B
</label>
</div>
<div class="choice">
<input id="choice-1c" type="radio" name="radio1" />
<label for="choice-1c">
<strong>C.</strong>
Option C
</label>
</div>
<div class="choice">
<input id="choice-1d" type="radio" name="radio1" />
<label for="choice-1d">
<strong>D.</strong>
Option D
</label>
</div>
<div class="choice">
<input id="choice-1e" type="radio" name="radio1" />
<label for="choice-1e">
<strong>E.</strong>
Option E
</label>
</div>
</div>
<div class="show-answer-button-container">
<label class="show-answer">
<fieldset hidden class="answers">
<strong>Correct Answer: </strong>
A
</fieldset>
<button type="button" onclick="checkAnswers()">Check Answers</button>
</label>
</div>
</div>
</div>
</div>
Provided below is the CSS code portion:
.selectt {
display: none;
}
.questions {
display: flex;
justify-content: center;
border: 0.3px solid gray;
padding: 10px;
}
.questions-left-column {
flex-direction: column;
border-right: 1px solid black;
width: 400px;
padding-right: 100px;
}
.question-area {
margin-bottom: 30px;
border: 0.3px solid gray;
padding: 10px;
}
.choice {
width: 350px;
padding: 0.5rem;
align-content: left;
justify-self: center;
border: 1px solid darkgrey;
}
.choice input[type="radio"]:checked + label {
padding: 0.5rem;
background: rgb(226, 182, 182);
cursor: default;
}
input {
visibility: hidden;
}
label {
cursor: pointer;
}
.show-answer-button-container {
margin: 10px;
}
.show-answer {
margin-top: 0.5rem;
align-items: right;
padding: 0.1rem;
border: 0.5px solid rgb(119, 108, 108);
background: rgb(133, 183, 212);
color: white;
border-radius: 0.2rem;
}
Lastly, here is the JS code section provided:
function checkAnswers() {
var elements = document.getElementsByClassName("choice answer");
for (var i = 0; i < elements.length; i++) {
elements[i].style.backgroundColor = "lightgreen";
}
}
The issues that I am currently facing include:
After selecting a choice, only the background color of the text changes rather than the entire option box.
When clicking the 'Show Answer' button, the correct answer is displayed with a green background. However, if the chosen option matches the answer text's background, it turns purple while the box's background remains green.
I appreciate any assistance provided!