-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
31 lines (27 loc) · 1016 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const faqQuestions = document.querySelectorAll('.faq-question');
faqQuestions.forEach(question => {
question.addEventListener('click', () => {
const answer = question.nextElementSibling;
// If the answer is already open, close it
const isActive = answer.classList.contains('active');
answer.classList.toggle('active', !isActive);
question.setAttribute('aria-expanded', !isActive);
// Toggle maxHeight based on active class
if (!isActive) {
answer.style.maxHeight = answer.scrollHeight + 'px';
} else {
answer.style.maxHeight = '0';
}
// Close other answers
faqQuestions.forEach(otherQuestion => {
if (otherQuestion !== question) {
const otherAnswer = otherQuestion.nextElementSibling;
if (otherAnswer.classList.contains('active')) {
otherAnswer.classList.remove('active');
otherAnswer.style.maxHeight = '0';
otherQuestion.setAttribute('aria-expanded', 'false');
}
}
});
});
});