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

169
Visualizações
Automatically hide other div in Javascript
const answer = document.querySelectorAll(".answer");
const question = document.querySelectorAll(".question");

console.log(question);
for (let i = 0; i < question.length; i++) {
  question[i].addEventListener("click", function () {
    answer[i].classList.toggle("hide");
  });
}

I have a FAQ with questions and answers. The answers are hidden until the question is clicked. How can I make it that when i have a answers showing, it automatically hides when i Click on a other question so only one answer can be showing?

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

0

Loop through all the answers. If it's the answer to the current question, toggle it, otherwise hide it.

for (let i = 0; i < question.length; i++) {
  question[i].addEventListener("click", function () {
    for (let j = 0; j < answer.length; j++) {
      if (j == i) {
        answer[j].classList.toggle("hide");
      } else {
        answer[j].classList.add("hide");
      }
    }
  });
}

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