Accordion Tab does not open on Keyboard Input

In Germany, websites must be made accessible by June due to the Barrier-Free Strengthening Act (Barrierefreiheitsstärkungsgesetz). This includes ensuring that accordions can be opened using the keyboard.

By default, selecting accordion elements via the Tab key was not possible. However, I managed to solve this by adding the following ARIA attributes to the accordion items, allowing them to be selected in sequence using the Tab key :

role=button
aria-expanded=false
aria-controls=panel-1
tabindex=0

However, opening and closing the accordion using the Enter key does not work.

Interestingly, both selection and opening work perfectly fine with the modal elements in the theme, but not with accordions .

How can this issue be resolved?

2. Search Issue:
Additionally, content within the accordions does not appear in search results when using the site’s search field. This is a critical issue for the website, as important information is hidden from the search index.

Attached are screenshots for better understanding.

I am looking forward to your response and a possible solution. Best regards Daniel
Tried here: https://www.grabow.de/leben-im-amt/wohnenbauen/

Pro Version: 6.5.14 | WordPress: 6.7.1

Hello @darood87,

Thank you for the inquiry.

There is no script available for this by default, but you can try this function in the Custom Code > Global JS field:

  function initializeAccordionKeyPress() {
    const accordionItems = document.querySelectorAll('.x-acc-item');
    
    accordionItems.forEach((item) => {
      item.addEventListener('keydown', (event) => {
        if (event.key === 'Enter') {
          const button = item.querySelector('.x-acc-header');
          button.click();
        }
        
        if (event.key === 'Tab') {
          return;
        }
      });
    });
  }

  document.addEventListener('DOMContentLoaded', initializeAccordionKeyPress); 

This should toggle the accordion items when the Enter key is pressed. Let us know the results.

Best regards,
Ismael

Hello Ismael,
thank you for your quick reply.

Your custom Code didn’t work but I made some changes and now it is possible to open and close the accordion items.

Here is the new JS-Code:
function initializeAccordionKeyPress() {
const accordionItems = document.querySelectorAll(’.x-acc-item’);

accordionItems.forEach((item, index) => {
    const button = item.querySelector('.x-acc-header');
    const content = item.querySelector('.x-acc-content');
    const chevronIcon = button.querySelector('svg, i'); 

    // ARIA-Attribute korrekt initialisieren
    button.setAttribute('role', 'button');
    button.setAttribute('tabindex', '0'); // Nur der Button ist erreichbar
    button.setAttribute('aria-expanded', 'false');
    button.setAttribute('aria-controls', `panel-${index}`);
    content.setAttribute('id', `panel-${index}`);
    content.setAttribute('aria-hidden', 'true');
    content.setAttribute('tabindex', '-1'); // Inhalt nicht fokussierbar, wenn geschlossen

    // Entferne den Fokus vom Chevron (rein dekorativ)
    if (chevronIcon) {
        chevronIcon.setAttribute('aria-hidden', 'true');
        chevronIcon.setAttribute('tabindex', '-1');
    }

    // 🎯 Klick- und Tastatursteuerung für den Button
    button.addEventListener('keydown', (event) => {
        if (event.key === 'Enter' || event.key === ' ') {
            event.preventDefault();
            button.click();
        }
    });

    // 🎯 Klick-Event für den Button
    button.addEventListener('click', () => {
        const isExpanded = button.getAttribute('aria-expanded') === 'true';

        // Nur Button bleibt fokussierbar
        button.setAttribute('aria-expanded', !isExpanded);
        content.setAttribute('aria-hidden', isExpanded);
        
        // **Tabindex nur aktiv, wenn geöffnet**
        if (!isExpanded) {
            content.setAttribute('tabindex', '0');
            content.focus();
        } else {
            content.setAttribute('tabindex', '-1');
            button.focus(); // Fokus zurück auf Button
        }
    });
});

}

// Sicherstellen, dass der Code erst nach DOM-Ladung ausgeführt wird
document.addEventListener(‘DOMContentLoaded’, initializeAccordionKeyPress);

Global CSS
/* Deutlicher Fokusrahmen für den Button */
.x-acc-header:focus {
outline: 2px solid #1B9246;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}

/* Unsichtbare Inhalte dürfen nicht fokussierbar sein /
.x-acc-content[aria-hidden=“true”] {
display: none; /
Unsichtbar und nicht erreichbar */
pointer-events: none;
visibility: hidden;
}

.x-acc-content[aria-hidden=“false”] {
display: block; /* Sichtbar, wenn geöffnet */
pointer-events: auto;
visibility: visible;
}

Unfortunately, I still have an issue:
The focus outline for the collapsed element is not visible. Here’s an example:

  1. I press Tab : A blue focus outline appears on the accordion item (but it cannot yet be opened with Enter).

  2. I press Tab again: The focus outline selects the hidden element within the first accordion item, but it is not visible (it can now be opened with Enter).

  3. I press Tab once more: It selects the next accordion item, which can be opened immediately with Enter (again, no visible focus outline).

To make it fully accessible, it would be ideal if the focus outline was always visible after each selection. Unfortunately, I couldn’t solve this issue with CSS either.

Any solutions?

Thank you for the update.

The previous function works fine on our end. You can see it in action in this short clip: https://drive.google.com/file/d/1rVUYaPfKYXeRJHK-hOJpOn6sSvixZXMQ/view?usp=sharing

Would you mind providing a short clip of the issue you’re describing? If you require further assistance with this type of customization, you might consider subscribing to our One service.

Warm regards.

Hello, thanks for the video. That’s what I need.
I made a short clip of the problem on a fresh site and still have the same problem.
Tested on Safari and Chrome for Mac.

Video:

1 Like

Thank you for following up.

We tested the initial function again on Chrome and Firefox, and it is working as expected. We may need to access the site to check the issue properly. Please provide the login details in the secure note.

Warm regards.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.