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

130
Visualizações
Quiz app that cycles through the divs on the click of the next button

I am new to javascript and am working on a quiz project. I am trying to loop through the divs so that they show up one by one on the click of the next button. For some reason it skips the second div.

<div id='question-one' class='questions active-question'></div>
<div id='question-two' class='questions active-question'></div> 
<div id='question-three' class='questions active-question'></div>
const nextButton = document.getElementById('myButton');
        
        nextButton.addEventListener('click', setNextQuestion);

        function setNextQuestion() {

            var isNowTheActiveQuestion = document.getElementsByClassName('questions');

            for (activeQuestion of isNowTheActiveQuestion) {
                activeQuestion.classList.remove('active-question');
            } 
            activeQuestion.classList.add('active-question');   
        }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You don't need to give "active-question" class for all items. You should give just what item you want to see first.

 <div id='question-one' class='questions active-question'></div>
  <div id='question-two' class='questions'></div> 
  <div id='question-three' class='questions'></div>

Now come to the Js code

const nextButton = document.getElementById('myButton');

    nextButton.addEventListener('click', setNextQuestion);

    let count = 1;
    function setNextQuestion() {
        const isNowTheActiveQuestion = document.getElementsByClassName('questions');
        for(let i = 0; i < isNowTheActiveQuestion.length; i++){
          isNowTheActiveQuestion[i].classList.remove('active-question');
        };
        if(count > isNowTheActiveQuestion.length - 1){
          count = 0;
        };
        isNowTheActiveQuestion[count++].classList.add('active-question');
    };
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