I think there’s a mistake in how you’re using the ternary operator:
resposta == value ? mensagem = "Parabéns, Acertou!" : mensagem = "Resposta errada, tente novamente";
Try changing this to:
mesagem = resposta == value ? 'Parabéns, Acertou!' : 'Resposta errada, tente novamente';
Also, you should use <input type='checkbox'>
for multiple choice, and for the right answers, Voce can use an array, its HTML can be like this:
<p><input type="checkbox" name="questao" value="A">resposta 1</p>
<p><input type="checkbox" name="questao" value="B">resposta 2</p>
<p><input type="checkbox" name="questao" value="C">resposta 3</p>
<p><input type="checkbox" name="questao" value="D">resposta 4</p>
<p id="mensagem"></p>
With this, your JS can change to:
var respostasCertas = ['A', 'D'];
var respostasEscolhidas = [];
$('input[name="questao"]').on('change', function() {
if(this.checked) {
respostasEscolhidas.push($(this).val());
} else {
removeA(respostasEscolhidas, $(this).val());
}
});
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
Above, I use a function from another question, to remove array position by value
To check the answer Voce can add one <a>
or a submit
<a onclick="checkResponse()">Checar resposta</a>
And finally create a function that checks the answer:
function checkResponse() {
if (respostasCertas.length === respostasEscolhidas.length && respostasCertas.sort().every(function(value, index) { return value === respostasEscolhidas.sort()[index]})) {
$('#mensagem').text('VOCE ACERTOU');
} else {
$('#mensagem').text('TENTE NOVAMENTE');
}
}
The above function has been removed of that question
I tested this code and I think it’s exactly what you’re looking for, I hope I’ve helped!
do you want to be able to select more than one option? or just allow one of the correct answers to be chosen?
– Alvaro Alves
If they are more of a correct answer, you should use the checkbox. You could [Dit] the question and add how you did with these elements to understand why it didn’t work?
– Woss
Be able to select more than one option and at the time already appear whether the answer is right or wrong
– Thi100
@Thi100 try to post more information from your code
– Dimitrius Lachi
@Thi100 Poseti a code using checkbox to solve the problem
– Dimitrius Lachi