3
I have a table in Django where all the answers of a test are stored, but when I select them to compare with the answer provided by the user, all the answers are the same as the first question. I know this has to do with getElementsById, which only selects the first element. But how do I select all elements of the same id in HTML?
{% for resposta in questao.resposta_set.all %}
<input type="hidden" id="resposta" name="resposta" value="{{resposta.resposta}}">
<script type="text/javascript">
function mostrarSel() {
if (getRadioValor('opcao_escolhida') == document.getElementById("resposta").value) {
alert('Resposta Correta');
}
else{
alert('Resposta Incorreta');
}
}
function getRadioValor(name){
var rads = document.getElementsByName(name);
for(var i = 0; i < rads.length; i++){
if(rads[i].checked){
return rads[i].value;
}
}
return null;
}
</script>
{% endfor %}
id
should be unique in the DOM. If you have several elements with the sameid
, you did something wrong.– Woss
Yes, I have science that I made wrong. Now I would like to know what I must do to fix this mistake.
– Murilo de Jesus
Right. What would those JS functions be within the
for
and when are they called? With this information, I believe I will be able to answer what you need.– Woss
Sorry for the delay. I am comparing a value of one of the options of a select with a value that is saved in the Bank
– Murilo de Jesus