I’d like to create a very simple html quiz, with the less code possible.
I think a 3/4 radio buttons, only one of them with a given class (.correct), and a js to display a text (correct answer!) if that radio button is checked.
Something like this:
<p>my question</p>
<input type="radio" name="1"> a. first answer<br>
<input type="radio" name="1" class="correct"> b. second answer<br>
<input type="radio" name="1"> c. third answer<br>
<p class="result">
<span class="correct_ans">correct answer!</span>
<span class="wrong_ans">wrong answer!</span>
</p>
And javascript should show .correct_ans
if the second answer is checked, otherwise .wrong_ans
Thank you!
EDIT
I have found this code
function ShowHideDiv() {
var chkYes = document.getElementById("chkYes");
var dvtext = document.getElementById("dvtext");
dvtext.style.display = chkYes.checked ? "block" : "none";
}
<label for="chkYes">
<input type="radio" id="chkYes" name="chk" onclick="ShowHideDiv()" />
Yes
</label>
<label for="chkNo">
<input type="radio" id="chkNo" name="chk" onclick="ShowHideDiv()" />
No
</label>
<div id="dvtext" style="display: none">
Text Box:
<input type="text" id="txtBox" />
</div>
It is almost what I need, but I need to work with classes and not with IDs, because I plan to have several quiz in the same page.
I tried this code, but unsuccessfully:
<label for="chkYes">
<input type="radio" class="chkYes" name="chk" onclick="ShowHideDiv()" />
Yes
</label>
<label for="chkNo">
<input type="radio" class="chkNo" name="chk" onclick="ShowHideDiv()" />
No
</label>
<div class="dvtext" style="display: none">
correct!
</div>
<script>
function ShowHideDiv() {
var chkYes = document.getElementsByClassName('chkYes');
var dvtext = document.getElementsByClassName('dvtext');
dvtext.style.display = chkYes.checked ? "block" : "none";
}
</script>
EDIT
Maybe I should change completely the code…