This is a basic structure for a single multiple choice question. The JavaScript function checkAnswer()
checks the selected radio button and displays an explanation based on whether the answer is correct or not.
- Duplicate the structure for each question. Make sure to change the
name
attribute of the radio buttons for each question so that the browser treats them as separate groups. - Update the JavaScript function to handle each question separately. You’ll need a way to identify which question is being answered and provide the correct feedback.
- Manage the explanations for each question. This can become quite lengthy, so you may want to consider loading explanations dynamically or using a more advanced method of displaying them.
Please note that this is a basic implementation. For a full-fledged quiz application, you might want to consider more advanced features like randomizing questions, keeping track of scores, or even storing user progress, which would require a more complex setup possibly involving a backend and a database.
<!DOCTYPE html>
<html>
<head>
<title>Quiz</title>
</head>
<body>
<h2>Question 1</h2>
<form>
<label>
<input type="radio" name="question1" value="A"> Option A
</label><br>
<label>
<input type="radio" name="question1" value="B"> Option B
</label><br>
<label>
<input type="radio" name="question1" value="C"> Option C
</label><br>
<label>
<input type="radio" name="question1" value="D"> Option D
</label><br>
<input type="button" value="Submit" onclick="checkAnswer()">
</form>
<p id="explanation" style="display:none;">
<!-- Explanation here -->
</p>
<script>
function checkAnswer() {
var selectedOption = document.querySelector('input[name="question1"]:checked').value;
var explanation = document.getElementById("explanation");
if (selectedOption == "C") { // Assuming the correct answer is Option C
explanation.innerHTML = "Correct! Here's why: [explanation for why C is correct]";
} else {
explanation.innerHTML = "Incorrect. The correct answer is C. Here's why: [explanation for why C is correct]";
}
explanation.style.display = "block";
}
</script>
</body>
</html>