Many thanks.
I’m working locally with ES6 module.
The base of the JavaScript code is:
const elAccordianButon = document.querySelector('.my-custom-accordian-item-class button')
const elAccordianPanel = document.querySelector(".my-custom-accordian-item-class [id^='panel']")
function fncCollapseAccordian() {
elAccordianPanel.classList.add('x-collapsed')
elAccordianPanel.setAttribute('aria-hidden', 'true')
elAccordianButon.classList.remove('x-active')
elAccordianButon.setAttribute('aria-selected', 'false')
elAccordianButon.setAttribute('aria-expanded', 'false')
}
const elAccordianCollapse = document.querySelector('.my-custom-collapse-class')
if (elAccordianCollapse) {
elAccordianCollapse.addEventListener('click', fncCollapseAccordian)
}
The steps are:
- add a custom class to the accordion item element in the builder
- Inside the accordion content add a collapse link class which can, either as a class to a button or simply a span tag in the content like:
<span class="my-custom-collapse-class">Custom Collapse Link</span>
As you can see the Javascript has:
two variables, for the button and panel
a function which add/removes classes accordingly on clicking my collapse link inside the accordion content.
a variable for the content collapse link class
a listener event for the on click event to call the function.
It only needs to invoke a collapse as the collapse link is inside the content so it can never expand the panel.
As I said, it works, however if the accordion is collapsed with my content Custom Collapse Link the accordion header button needs to be clicked two times to expand again.
Not using Custom Collapse Link and just using the accordion header button the panel expands correctly with only one click of the header.