AI Hints
Home›JavaScript Tutorial›Practice Projects 4
JavaScript · Lesson 124 of 138

Practice: Tabs

A classic component.

⏱ 2 min read▶ Video at 2:28:51🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Tabs are everywhere. Settings pages, product details, dashboards. Click a tab, and its panel appears, while the others hide. It's a perfect small component to build with classes, data attributes, and one click listener.

Practice: Tabs
From the video: Practice: Tabs
  • Tabs are everywhere
  • Click a tab, see its panel
  • Classes and data attributes

Tabs

Each button has a data tab attribute that names its panel. One listener on the tab bar handles every click. It toggles an active class on each button, and hides every panel except the one whose i d matches. Toggle with a second argument adds the class when it's true, and removes it when it's false. We clicked the Reviews tab.

index.htmlpython
<link rel="stylesheet" href="style.css">
<style>.active { outline: 3px solid #333 } [hidden] { display: none }</style>
<div id="tabs">
  <button data-tab="info" class="active">Info</button>
  <button data-tab="specs">Specs</button>
  <button data-tab="reviews" id="rev">Reviews</button>
</div>
<p id="info" class="box">A light, strong backpack.</p>
<p id="specs" class="box" hidden>20 litres, 600 grams.</p>
<p id="reviews" class="box" hidden>★★★★★ "Perfect for hiking!"</p>
<script>
  tabs.addEventListener("click", (e) => {
    const name = e.target.dataset.tab;
    if (!name) return;
    for (const b of tabs.children) b.classList.toggle("active", b === e.target);
    for (const p of document.querySelectorAll("p")) p.hidden = p.id !== name;
  });
</script>
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#f7df1e;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}
Also uses actions.json
[{"click": "#rev"}]

Accessible components

Real tab components also think about keyboards and screen readers. They use roles, like role equals tab, and let people move between tabs with the arrow keys. The hidden attribute is a simple, built in way to hide elements, and screen readers respect it too.

keyvalue
data-tabwhich panel
toggle(cls, bool)add or remove
hiddenhide an element
Accessibilityroles, keyboard

Summary

The tabs used data attributes, one delegated listener, class toggling, and the hidden attribute. Next, a dark mode switch that remembers your choice.

Key takeaways
  • data-* attributes
  • One click listener
  • hidden panels
Next up: Practice: Dark Mode — a remembered setting