How to select all elements of the same id in Javascript in Django

Asked

Viewed 834 times

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 same id, you did something wrong.

  • Yes, I have science that I made wrong. Now I would like to know what I must do to fix this mistake.

  • 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.

  • 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

1 answer

1

Your code is not written correctly, you should not create a script inside the loop, that js is affecting not only the loop HTML, but everything that was rendered on the page.

The correct logic would be to separate HTML from Javascript:

HTML

    {% for resposta in questao.resposta_set.all %}
        <div class="questao">
            <input type="hidden" name="resposta" value="{{resposta.resposta}}">
        </div>
    {% endfor %}

Javascript (with jQuery)

var questoes = $('.questao');
$.each(questoes, function(num, questao){
    // sua lógica: 
    if (questao.find("name=[resposta]").val() == questao.find("name=[opcao]")){
        alert('opção correta');
    }
});

I don’t know how your HTML code is, you just put a snippet. In this case I created a loop using jQuery that checks every question and fires a alert when it’s right. You can customize this, or even have an action fired when the user selects an option and the code checks only that answer.

  • Sorry to keep you waiting. I’ll put my code here so you can help me better as I’m lost. https://pastebin.com/BemsxnVH

  • @Murilodejesus my answer already presents the logic that you should use to elaborate your code. Javascript should be outside of HTML, creates a arquivo.js responsible for validating your questions, if you do not know how to use jQuery, recommend to look into the subject, or to conduct new questions specifically on your question. Remembering that here no one will do the work for you, just help in questions and direction.

  • In fact, my answer already answers your question that is a problem of structuring and logic. If you have another question, elaborate a new question specifically and close it. Modifying the current question may lead to the closing of the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.