Loading workspace environment...
Connect HTML, CSS, and JavaScript together using querySelector, addEventListener, dynamic class toggling, and data-* attributes.
document.querySelector(".btn").addEventListener("click", (e) => { ... });Attaches event listener to DOM element.element.classList.toggle("active");Dynamically toggles CSS class on element.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Tab Component</title>
<style>
body { font-family: system-ui, sans-serif; padding: 2rem; background: #f1f5f9; }
.tab-widget { background: white; border-radius: 12px; border: 1px solid #cbd5e1; overflow: hidden; max-width: 480px; margin: 0 auto; box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.05); }
.tab-nav { display: flex; border-bottom: 1px solid #e2e8f0; background: #f8fafc; }
.tab-btn { flex: 1; padding: 1rem; border: none; background: transparent; cursor: pointer; font-weight: 600; color: #64748b; border-bottom: 2px solid transparent; transition: all 0.2s; }
.tab-btn.active { color: #2563eb; border-bottom-color: #2563eb; background: white; }
.tab-pane { padding: 1.5rem; display: none; line-height: 1.6; color: #334155; }
.tab-pane.active { display: block; }
</style>
</head>
<body>
<div class="tab-widget">
<div class="tab-nav">
<button class="tab-btn active" data-tab="tab-html">HTML</button>
<button class="tab-btn" data-tab="tab-css">CSS</button>
<button class="tab-btn" data-tab="tab-js">JavaScript</button>
</div>
<div id="tab-html" class="tab-pane active">
<h3>HTML5 Structure</h3>
<p>Semantic markup organizes content for users, machines, and accessibility tools.</p>
</div>
<div id="tab-css" class="tab-pane">
<h3>CSS3 Styling</h3>
<p>Cascading stylesheets give visual design, colors, typography, and responsive grid layouts.</p>
</div>
<div id="tab-js" class="tab-pane">
<h3>JavaScript Interactivity</h3>
<p>Dynamic scripts manipulate the DOM, fetch network data, and react to user events.</p>
</div>
</div>
<script>
const nav = document.querySelector('.tab-nav');
const panes = document.querySelectorAll('.tab-pane');
const buttons = document.querySelectorAll('.tab-btn');
nav.addEventListener('click', (e) => {
const btn = e.target.closest('.tab-btn');
if (!btn) return;
const targetId = btn.dataset.tab;
buttons.forEach(b => b.classList.remove('active'));
panes.forEach(p => p.classList.remove('active'));
btn.classList.add('active');
document.getElementById(targetId).classList.add('active');
});
</script>
</body>
</html>Renders an interactive tab widget where clicking tabs switches the active content view seamlessly.element.style.color = "blue"; element.style.fontSize = "20px"; element.style.border = "1px solid blue";element.classList.add("highlighted-state");Architectural Explanation: Avoid setting inline styles from JavaScript. Toggle CSS classes to maintain clean separation of concerns and enable GPU-accelerated styling.
Build a collapsible FAQ accordion item with a toggle button that expands and collapses the answer paragraph using classList.toggle().