/* Functionality for custom accordions */ /* Created by: Tim Jeffreys and Adil Sami (Dynamic Integrated Services) Created for: Veterans Affairs Created on: 2023-06-16 Last updated: 2024-04-18 Version: 1.1.1 PURPOSE: Finds all .accordion-custom blocks on the page and adds attritbutes and listeners to ensure each is accessible and generally functional. Also included is the optional show/hide toggle augmentation for pages where such a toggle is desired to expand and collapse all accordions in one click. Style note: variables are prefixed with their type to improve understanding of structure and interactions. Example: nl = nodeList, el = Element, arr = Array UPDATES: v1.1.0 - Included show/hide all toggle and adjusted variable naming style */ //-A- Revise all .accordion-custom blocks (and children) with appropriate attributes and functionality const nlAccordions = document.getElementsByClassName("accordion-custom"); //Iterate through all accordions for(let a = 0; a < nlAccordions.length; a++) { nlAccordions[a].setAttribute("aria-label","Accordion group " + (a + 1)); let elThisContent = nlAccordions[a].querySelector("div"); elThisContent.setAttribute("id", nlAccordions[a].getAttribute("id") + "-content"); elThisContent.setAttribute("aria-expanded","false"); let elAccBtn = nlAccordions[a].querySelector(":first-child > button"); elAccBtn.setAttribute("aria-pressed","false"); elAccBtn.setAttribute("aria-controls", nlAccordions[a].getAttribute("id") + "-content") elAccBtn.addEventListener("click", function(e) { e.stopPropagation(); //Prevents click from getting dampened by child elements of e.target. let elMyBtn; if(e.target.nodeName != "BUTTON") { elMyBtn = e.target.closest("h4 > button"); console.log(elMyBtn); }else{ elMyBtn = e.target; console.log(elMyBtn); } if(elMyBtn.getAttribute("aria-pressed") == "false") { elMyBtn.setAttribute("aria-pressed","true"); elMyBtn.parentElement.nextElementSibling.setAttribute("aria-expanded","true"); }else{ elMyBtn.setAttribute("aria-pressed","false"); elMyBtn.parentElement.nextElementSibling.setAttribute("aria-expanded","false"); } }, false); } //-B- ADD-ON: Optional Toggle to Show/Hide All // Show all accordions on click of a single button. Targets