Your checkCurrentAnswer
function is calculating the wrong index. Specifically, this line:
var i = $('#Answers input:checked').parent().index();
Your HTML looks like this:
<div id="Answers">
<div class="answer">
<span class="radioContainer"><input type="radio" /></span>
<label for="answer_0">The Musée du Louvre, Paris</label>
</div>
<div class="answer">
<span class="radioContainer"><input type="radio" /></span>
<label for="answer_1">The Tate Britain, London</label>
</div>
...
</div>
Thus, i
will always be 0
, since the parent of the radio
input is always a span
, which is always the first child (index 0
) of answer
. You should count the index
of the div with class answer
instead.
To fix, it’s simple, instead of using parent()
, use closest(".answer")
:
var i = $('#Answers input:checked').closest(".answer").index();
2
solved Score is not counting correctly based on whether the correct answer [closed]