Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Tabs/Accordion in JavaScript

Switch course sections in JavaScript with hidden and aria-pressed, plus native details for FAQs. Toggle buttons are not a complete arrow-key tab widget.

BeginnerJavaScript Mini ProjectsExample 9 of 20

Save this as index.html and open it in a browser.

tabs-accordion.js
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<title>JavaScript practice</title>
5<body>
6<h1>Course information</h1><div id="buttons"><button type="button" aria-controls="schedule" aria-pressed="true">Schedule</button><button type="button" aria-controls="fees" aria-pressed="false">Fees</button></div><section id="schedule">Classes start at 10 AM.</section><section id="fees" hidden>Practice sessions are free.</section><details><summary>What should I bring?</summary><p>A notebook and your laptop.</p></details><details><summary>Can I practise at home?</summary><p>Yes. Save the examples as HTML files.</p></details>
7<script>
8const buttons = [...document.querySelectorAll('#buttons button')];
9for (const button of buttons) {
10 button.onclick = () => {
11 for (const item of buttons) {
12 const active = item === button;
13 item.setAttribute('aria-pressed', String(active));
14 document.getElementById(item.getAttribute('aria-controls')).hidden = !active;
15 }
16 };
17}
18</script>
19</body>
20</html>

Sample Output

Practice sessions are free.

Clicking Fees reveals that section and hides Schedule.

A notebook and your laptop.

The browser opens this details answer when its summary is activated.

In this one

  • Switch between related page sections
  • Keep button state in sync
  • Use native details for expandable answers
  • Distinguish toggle buttons from a full tab widget

How it works

  1. 1

    Create two sections with unique IDs.

  2. 2

    Connect each button to its section using aria-controls.

  3. 3

    On click, mark one button pressed and hide the other section.

  4. 4

    Let details and summary handle the expandable answers.

  5. 5

    Read the Fees section and open the notebook answer independently.

Edge cases

Mismatched section ID

A button may point to an element that does not exist.

Keep aria-controls and section IDs identical. getElementById otherwise returns null.

Both answers needed

Closing one details element automatically is not required here.

These questions are independent. Multiple answers may stay open at the same time.

What's going on

Tabs switch between related sections, while an accordion opens and closes short sections. This page shows both patterns with course information. Use switching sections when only one view is needed; use expandable questions when people may want several answers open together.

The first two buttons control sections by ID. Clicking a button loops over both controls, marks the chosen one pressed, and hides the other section. aria-controls connects a control to the section it changes, and aria-pressed describes this example's button state to assistive tools.

The expandable questions use details and summary, which already provide built-in open and close behavior. No extra click handler is needed for them. The first pattern deliberately stays a set of toggle buttons, not a complete ARIA tab widget. ARIA is a set of attributes that describe interface meaning to assistive software. A production tab widget also needs the matching roles and keyboard-arrow behavior; do not add a tab role without that behavior.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What hides the inactive section?

Pick an answer to check it.

Question 2

Which element is the clickable accordion heading?

Pick an answer to check it.

Questions people ask

Why use details for the accordion?

It already supports expanding with mouse and keyboard, reducing code for a beginner example.

Are the first buttons a complete accessible tab widget?

No. They are ordinary toggle buttons. A full tab widget needs additional roles and keyboard behavior.

Can an open answer stay visible when I switch Fees?

Yes. The details elements are separate from the two switchable sections.