Build an Accessible Vanilla JavaScript Accordion for WordPress FAQ Sections

An accessible FAQ accordion without jQuery

An FAQ section shows the questions and opens one answer at a time. It is often built with a jQuery plugin or a page builder widget, but the browser already has a native disclosure element, <details>, and the button version with ARIA attributes takes about thirty lines of plain JavaScript.

Problem: The FAQ page of a theme needs collapsible answers that work with the keyboard and screen readers, without jQuery and without hiding the answers from visitors when JavaScript fails.

Solution: Use <details> and <summary> when the default behaviour is enough. When the design needs full control, use a <button aria-expanded> for each question and toggle the hidden attribute of the answer, with one delegated click handler for the whole list.

The native version

<div class="ha-faq">
    <details>
        <summary>Do you ship outside the EU?</summary>
        <p>Yes, to the UK, Switzerland and Norway.</p>
    </details>
    <details>
        <summary>How long does delivery take?</summary>
        <p>Two to four working days.</p>
    </details>
</div>
.ha-faq summary {
    cursor: pointer;
    font-weight: 600;
    padding: 0.75rem 0;
}

.ha-faq details[open] summary {
    color: var(--ha-accent, #0057b8);
}

The keyboard, the open state and the screen reader announcement (“collapsed”, “expanded”) are built in, the answers are found by the browser’s page search, and nothing breaks without JavaScript. Several answers can be open at the same time.

The button version

<div class="ha-accordion" data-single>
    <h3>
        <button type="button" aria-expanded="false" aria-controls="faq-1">Do you ship outside the EU?</button>
    </h3>
    <div id="faq-1" hidden>
        <p>Yes, to the UK, Switzerland and Norway.</p>
    </div>
    <h3>
        <button type="button" aria-expanded="false" aria-controls="faq-2">How long does delivery take?</button>
    </h3>
    <div id="faq-2" hidden>
        <p>Two to four working days.</p>
    </div>
</div>
document.querySelectorAll( '.ha-accordion' ).forEach( ( accordion ) => {
    const single = accordion.hasAttribute( 'data-single' );

    const setOpen = ( button, open ) => {
        button.setAttribute( 'aria-expanded', String( open ) );
        document.getElementById( button.getAttribute( 'aria-controls' ) ).hidden = ! open;
    };

    accordion.addEventListener( 'click', ( event ) => {
        const button = event.target.closest( 'button[aria-controls]' );
        if ( ! button ) {
            return;
        }
        const open = button.getAttribute( 'aria-expanded' ) !== 'true';
        if ( single && open ) {
            accordion.querySelectorAll( 'button[aria-expanded="true"]' ).forEach( ( other ) => setOpen( other, false ) );
        }
        setOpen( button, open );
    } );
} );

The heading around each button keeps the questions in the page outline, so screen reader users can jump between them. A <button> is focusable and reacts to Enter and Space by itself, no key handler is needed. data-single closes the other answers when one opens.

For the case without JavaScript, print the answers without hidden in PHP and add it from the script on load; then the answers are visible if the script does not run.

Animation

@media (prefers-reduced-motion: no-preference) {
    .ha-accordion [id^="faq-"] {
        animation: ha-faq-open 0.2s ease-out;
    }
}

@keyframes ha-faq-open {
    from { opacity: 0; transform: translateY(-0.25rem); }
}

Animating max-height to a pixel value cuts off long answers and needs a measurement in JavaScript; a short fade is simpler and enough.

NOTE: The FAQ can also carry FAQPage structured data; see JSON-LD structured data with PHP, but the questions in the markup must be the same as on the page.

Update (2024): Chrome 120 and Safari 17.2 added the name attribute for <details>: elements with the same name form a group in which only one can be open, so the native version can be a one-at-a-time accordion too.