Skip to main content
01

Accordion

Easydomeventsevent delegationa11y

Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.

Build an accordion in plain HTML, CSS and JavaScript, with no framework. The markup and styles are ready: right now every panel is always visible. Make each heading show and hide its own panel.

Requirements

  • All panels start closed.
  • Clicking a section's heading button opens its panel, and clicking it again closes it.
  • Sections are independent: opening one does not close the others.
  • The + / − sign next to each heading reflects whether it is open. The CSS already switches it based on aria-expanded.

Accessibility

  • Each heading button has aria-expanded (true or false) and aria-controls pointing to its panel's id.
  • Each panel has role="region" and aria-labelledby pointing back to its button.
  • Closed panels are hidden from everyone, including screen readers, not just visually.

Note

The preview updates as you type. There are no automated tests for this challenge yet: open and close every section, then compare with the reference solution.

Follow-ups interviewers ask

  • Allow only one open section at a time.
  • Keyboard support: ArrowUp / ArrowDown move between headings, Home / End jump to the first and last.
  • Animate opening and closing without JavaScript measuring heights.
  • Turn it into a reusable function that builds the accordion from data, e.g. createAccordion(container, sections).

The approach

The state of each section lives in the DOM itself: aria-expanded on the button and hidden on the panel. There is no separate JavaScript state to keep in sync, and the attribute screen readers announce is the same one the code reads.

  1. In the HTML, give every button an id, aria-expanded="false" and aria-controls, and every panel a matching id, role="region", aria-labelledby and the hidden attribute.
  2. In JavaScript, on click, read aria-expanded, flip it, and set panel.hidden to match.

Event delegation

Instead of one listener per button, a single listener on .accordion handles every click. event.target.closest('.accordion-trigger') finds the button even when the click lands on text inside it. This scales to any number of sections and keeps working for sections added later.

Loading diagram...

Why hidden and not a CSS class

hidden removes the panel from the layout and from the accessibility tree, so screen readers skip closed content. A class that only sets opacity: 0 or height: 0 would still let screen readers read it, and keep its links focusable.

What interviewers look for

  • Semantic HTML: real <button> elements inside headings, not clickable <div>s.
  • Correct ARIA wiring, with aria-expanded as the single source of truth.
  • Event delegation instead of a listener per element.
  • A clear answer to "only one open at a time": close every other trigger before opening the clicked one.

Reference solution

index.html

html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Accordion</title>
  </head>
  <body>
    <div class="accordion">
      <div class="accordion-item">
        <h3 class="accordion-heading">
          <button
            type="button"
            class="accordion-trigger"
            id="accordion-trigger-1"
            aria-expanded="false"
            aria-controls="accordion-panel-1"
          >
            HTML
          </button>
        </h3>
        <div
          class="accordion-panel"
          id="accordion-panel-1"
          role="region"
          aria-labelledby="accordion-trigger-1"
          hidden
        >
          HTML gives a page its structure: headings, paragraphs, lists, forms and the meaning
          behind them.
        </div>
      </div>
      <div class="accordion-item">
        <h3 class="accordion-heading">
          <button
            type="button"
            class="accordion-trigger"
            id="accordion-trigger-2"
            aria-expanded="false"
            aria-controls="accordion-panel-2"
          >
            CSS
          </button>
        </h3>
        <div
          class="accordion-panel"
          id="accordion-panel-2"
          role="region"
          aria-labelledby="accordion-trigger-2"
          hidden
        >
          CSS controls how that structure looks: layout, color, spacing, typography and
          animation.
        </div>
      </div>
      <div class="accordion-item">
        <h3 class="accordion-heading">
          <button
            type="button"
            class="accordion-trigger"
            id="accordion-trigger-3"
            aria-expanded="false"
            aria-controls="accordion-panel-3"
          >
            JavaScript
          </button>
        </h3>
        <div
          class="accordion-panel"
          id="accordion-panel-3"
          role="region"
          aria-labelledby="accordion-trigger-3"
          hidden
        >
          JavaScript makes the page interactive: it responds to events and updates the DOM.
        </div>
      </div>
    </div>

    <script src="src/index.js"></script>
  </body>
</html>

src/index.js

jsx
import './styles.css'

const accordion = document.querySelector('.accordion')

// One listener on the container handles every trigger, including ones added later.
accordion.addEventListener('click', (event) => {
  const trigger = event.target.closest('.accordion-trigger')
  if (!trigger || !accordion.contains(trigger)) return

  const expanded = trigger.getAttribute('aria-expanded') === 'true'
  const panel = document.getElementById(trigger.getAttribute('aria-controls'))

  trigger.setAttribute('aria-expanded', String(!expanded))
  panel.hidden = expanded
})