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

375
Visualizações
In Blogger, Hide/Show Quiz Answers based on button click with javascript

I have a quiz blog/website on blogger. To show the answers of questions I have following html code:

<button class="acc">Show Answer</button>
<div class="pnl">
  <p>Correct Answer</p>
</div>

<button class="acc">Show Answer</button>
<div class="pnl">
  <p>Correct Answer</p>
</div>

<button class="acc">Show Answer</button>
<div class="pnl">
  <p>Correct Answer</p>
</div>

And JavaScript like this:

<script type='text/javascript'>
     
//<![CDATA[ 
     var acc = document.getElementsByClassName("acc");
     var i;
    for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var pnl = this.nextElementSibling;
    if (pnl.style.display === "block") {
      pnl.style.display = "none";
    } else {
      pnl.style.display = "block";
    }
  });
}
    //]]>
 
</script>

And CSS like this:

.acc {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}
.active, .acc:hover {
  background-color: #ccc; 
} 
.pnl {
  padding: 0 18px;
  display: none;
  background-color: white;
  overflow: hidden;
}

If some one clicks on all of the buttons, the buttons are acting as toggles rather than, showing one answer, then when the next button is clicked, hiding the previous answer and showing the new one in its place. What needs to be changed to enable this?

Thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this one by using a forEach.

var acc = document.getElementsByClassName("acc");
let pn1 = document.getElementsByClassName("pnl");
   for (let i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
  this.classList.toggle("active");
  [...acc].forEach((item,index) =>{
  if(item == acc[i]){
  pn1[index].style.display = "block";
  }else{
   pn1[index].style.display = "none";
}
})
})
}
.acc {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}
.active, .acc:hover {
  background-color: #ccc; 
} 
.pnl {
  padding: 0 18px;
  display: none;
  background-color: white;
  overflow: hidden;
}
<button class="acc">Show Answer</button>
<div class="pnl">
  <p>Correct Answer</p>
</div>

<button class="acc">Show Answer</button>
<div class="pnl">
  <p>Correct Answer</p>
</div>

<button class="acc">Show Answer</button>
<div class="pnl">
  <p>Correct Answer</p>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. Remove the buttons.

  2. Add the following over each .pnl

   <!-- #ids must be unique so btn* = btn1, btn2, etc -->
   <!-- [for] of label must match #id of input -->
   <input id='btn*' class="acc" name='acc' type='radio' hidden>
   <label for='btn*'>Show Answer</label>
  • Explination: A label and a form control (ex. <input>, <select>, etc) can be associated with each other if the form control has an #id and the label has a [for] that match. If one gets clicked, checked, etc then the other one does as well.
  1. Add the following CSS:
     .acc:checked+label+.pnl {
        display: block
      }
  • Explination: If an input is checked then the .pnl that is front of the label that is in front of the input. Note, when a group of radio buttons share a [name] only one may be checked at a time. Also, the radio buttons are hidden so it looks as if the label is the only tag interacting with the user.

.acc {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}

.active,
.acc:hover {
  background-color: #ccc;
}

.pnl {
  padding: 0 18px;
  display: none;
  background-color: white;
  overflow: hidden;
}

.acc:checked+label+.pnl {
  display: block
}

label {
  display: block
}
<input id='btn1' class="acc" name='acc' type='radio' hidden>
<label for='btn1'>Show Answer</label>
<div class="pnl">
  <p>Correct Answer</p>
</div>

<input id='btn2' class="acc" name='acc' type='radio' hidden>
<label for='btn2'>Show Answer</label>
<div class="pnl">
  <p>Correct Answer</p>
</div>

<input id='btn3' class="acc" name='acc' type='radio' hidden>
<label for='btn3'>Show Answer</label>
<div class="pnl">
  <p>Correct Answer</p>
</div>

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