-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascript.js
31 lines (31 loc) · 1.63 KB
/
Javascript.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
/* Rankmath FAQs Accordion */
function initAccordion(accordionSelector, questionSelector, answerSelector) {
document.addEventListener('DOMContentLoaded', function () {
const accordion = document.querySelector(accordionSelector);
if (!accordion) return; // handle error if accordion not found
const questions = accordion.querySelectorAll(questionSelector);
questions.forEach((question) => {
const answer = question.nextElementSibling;
if (!answer || !answer.classList.contains(answerSelector.slice(1))) return; // handle error if answer not found
question.setAttribute('aria-expanded', 'false');
answer.style.maxHeight = 0;
question.addEventListener('click', function () {
// Close all open accordions
questions.forEach((q) => {
if (q !== this) {
q.setAttribute('aria-expanded', 'false');
q.classList.remove('active');
q.nextElementSibling.classList.remove('active');
q.nextElementSibling.style.maxHeight = 0;
}
});
// Toggle the clicked accordion
this.classList.toggle('active');
answer.classList.toggle('active');
answer.style.maxHeight = answer.classList.contains('active') ? answer.scrollHeight + 'px' : 0;
this.setAttribute('aria-expanded', this.classList.contains('active') ? 'true' : 'false');
});
});
});
}
initAccordion('#rank-math-faq', '.rank-math-question', '.rank-math-answer');