Skip to content

Instantly share code, notes, and snippets.

@bcnzer
Created May 26, 2020 10:07
Show Gist options
  • Save bcnzer/9dc6fe6d4e71b352b4fb08c0430fcc28 to your computer and use it in GitHub Desktop.
Save bcnzer/9dc6fe6d4e71b352b4fb08c0430fcc28 to your computer and use it in GitHub Desktop.
Simple HTML and JS quiz
<html>
<body>
<div class="question">
Q1: What is fun?
</div>
<div>
<input type="radio" id="a0" name="question0">
<label for="a0">Going for a walk</label>
</div>
<div>
<input type="radio" id="a1" name="question0">
<label for="a1">Netflix and chill</label>
</div>
<div>
<input type="radio" id="a2" name="question0">
<label for="a2">Hanging out with my cat</label>
</div>
<div class="question">
Q2: Best food?
</div>
<div>
<input type="radio" id="a3" name="question1" value="walk">
<label for="a3">Chips</label>
</div>
<div>
<input type="radio" id="a4" name="question1" value="netflix">
<label for="a4">Pizza</label>
</div>
<div>
<input type="radio" id="a5" name="question1" value="cat">
<label for="a5">Burgers</label>
</div>
<button onclick="getResults()">Get Results</button>
<button onclick="clearResults()">Clear Results</button>
<div id="results"></div>
<div id="img"></div>
<script>
function getResults() {
var score = 0
var answerQ1 = document.getElementById('a2')
var answerQ2 = document.getElementById('a5')
if (answerQ1.checked) score = score + 1
if (answerQ2.checked) score = score + 1
var resultsDiv = document.getElementById('results')
resultsDiv.innerHTML = "You scored " + score + " out of 2"
}
function clearResults() {
var resultsDiv = document.getElementById('results')
resultsDiv.innerHTML = ""
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment