Main website
Government Drupal Developers NetworkCommunity

AccessibilityAccessibility Solved

Accessible accordions: aria-expanded not updating in our custom theme

Started by Hannah Price on · 390 views · 3 replies · 9 likes

HP

Hannah PriceStarted the thread

Frontend Developer, NHS England ·

Our accordion component toggles visually but NVDA always announces "collapsed". Markup:

<button class="accordion__toggle" aria-controls="panel-1">Eligibility</button>
<div id="panel-1" class="accordion__panel" hidden>…</div>

The JS toggles a class and the hidden attribute. What am I missing?

Accepted answerSee it in the thread
SN

Sophie NguyenGroup lead

Accessibility Specialist, Digital Transformation Agency ·

You never set aria-expanded on the button, so screen readers have no state to announce. Start it at false and keep it in sync:

toggle.addEventListener('click', () => {
  const open = toggle.getAttribute('aria-expanded') === 'true';
  toggle.setAttribute('aria-expanded', String(!open));
  panel.hidden = open;
});

Also wrap the button in a heading (<h3>) so people can navigate by headings.

3 replies

  1. Sophie NguyenGroup lead

    Accessibility Specialist, Digital Transformation Agency ·

    #1

    Accepted answer

    You never set aria-expanded on the button, so screen readers have no state to announce. Start it at false and keep it in sync:

    toggle.addEventListener('click', () => {
      const open = toggle.getAttribute('aria-expanded') === 'true';
      toggle.setAttribute('aria-expanded', String(!open));
      panel.hidden = open;
    });
    

    Also wrap the button in a heading (<h3>) so people can navigate by headings.

  2. Priya ShahModerator

    Lead Drupal Developer, Government Digital Service ·

    #2

    Worth looking at the GOV.UK Frontend accordion too. It handles 'show all sections' and remembers state in session storage.

  3. Aisha Okafor

    Product Manager, Croydon Council ·

    #3

    Saving this for our content designers. We had the same bug in our FAQ pages.

Join the conversation

GDDN is for people working on Drupal in government. Log in or apply to reply.