Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda