Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
Count multiple div class in same page

I am trying to make a correct and incorrect question counter that shows groups of 4.

If I click on the first correct answer the counter works correctly and increases as I click, but it does not work with the second correct answer. The same happens with the wrong answers

This is the codes that I use, anyone could help me? Thx

HTML CODE:

    ¿Which of the following operations results in 8?
<input class="solucioncorrecta" value="6+2">
<input class="solucioncorrecta" value="7+1">
<input class="solucionincorrecta" value="1+1">
<input class="solucionincorrecta" value="2+2">

And the JS CODE:

<!-- CONTADOR FALLOS TEST -->
<script type="text/javascript">
var root = document.querySelector('.solucionincorrecta');

root.onclick = function() { 
var elem = document.getElementById('contadorfallos');
  elem.innerHTML = +elem.innerText + 1;
};
</script>
<!-- CONTADOR FALLOS TEST -->


<!-- CONTADOR ACIERTOS TEST -->
<script type="text/javascript">
var root = document.querySelector('.solucioncorrecta');

root.onclick = function() { 
var elem = document.getElementById('contadoraciertos');
  elem.innerHTML = +elem.innerText + 1;
};
</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The issue is that you are using document.querySelector() and not document.querySelectorAll()

document.querySelector() Returns the first match

document.querySelectorAll() Returns all matches

As a result, you are only setting an onclick property on the first .correcta and .incorrecta elements, not all of them.

To set this on all of them, you need to do two things:

  1. You need use document.querySelectorAll() instead of document.querySelector(). This returns a list (specifically, a NodeList) of matching elements.
  2. Loop over the items in your list, and attach onclick handlers to each of them. There are many ways to loop over a NodeList, listed here.

Here is an example:

// get all incorrect elements
var incorrectElements = document.querySelectorAll('.incorrecta');

// loop over each elements
for (var element of incorrectElements) {
  // add an onclick
  element.onclick = incorrectClickHandler
}

// this is the function being called by onclick
function incorrectClickHandler() {
  score.innerText = parseInt(score.innerText) - 1;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

It would be better if you upload your full codes. But anyway I write you some notes that probably answer your question.

-dont use the same name (root) for your .correcta and .incorrecta

-in your second <script>, you didnt defined button as an object . So browser cant understand it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda