FAQs not working properly

Hi everyone, I have created an FAQs section in a blog post. It went well though but there is an issue, When I click a question its answer appears but when I click the other one, the first one is supposed to close as the second one opens. But in my case, both stay open, in fact, all of them. I want one of them to be opened when I click it and when I click the next one I want the previous one to be closed automatically.

Any assistance would be appreciated.

Hi @rowandeclan100 Well doesn’t seem to be a big problem, it would have been easier if you had shared your code. Anyhow, here is a function that I have used

function toggleAnswer(index)
 { 
const answer = document.getElementById(`faq-answer-${index}` ); 
if (answer.style.display === "none" || answer.style.display === "")
 {
 answer.style.display = "block"; } else { answer.style.display = "none";
 }
 }

Make some adjustments according to your function and field names and hopefully it will work well.