-
Notifications
You must be signed in to change notification settings - Fork 0
/
accordion-single.js
25 lines (21 loc) · 1019 Bytes
/
accordion-single.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
const accordionButton = document.querySelector("[data-accordion-button]")
const accordionContent = document.querySelector(".accordion__content")
const accordionHeading = document.querySelector("[data-accordion-heading]")
// Button
accordionButton.setAttribute("aria-expanded", false)
accordionButton.addEventListener("click", () => {
accordionButton.classList.toggle("expanded")
accordionContent.classList.toggle("open")
if (accordionButton.classList.contains("expanded")) {
accordionButton.setAttribute("aria-expanded", true)
accordionContent.setAttribute("aria-hidden", false)
accordionHeading.setAttribute("tabindex", "0") // sets focus on first child (h3) with tabindex
} else {
accordionButton.setAttribute("aria-expanded", false)
accordionContent.setAttribute("aria-hidden", true)
accordionHeading.removeAttribute("tabindex") // removes focus from first child: (h3)
}
})
// Content
accordionContent.setAttribute("aria-hidden", true)
accordionContent.classList.remove("open")