Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!