<header>
  <h2 class="title">DOMTokenList: toggle() method</h2>
  <p class="description">Додає або видаляє токен з об'єкта DOMTokenList.</p>
</header>
<main>
  <div class="result">
    <div class="tabs">
      <div class="tab-headers">
        <button class="tab-header active" data-target="tab1">Вкладка 1</button>
        <button class="tab-header" data-target="tab2">Вкладка 2</button>
        <button class="tab-header" data-target="tab3">Вкладка 3</button>
      </div>
      <div class="tab-contents">
        <div class="tab-content active" id="tab1">Контент вкладки 1</div>
        <div class="tab-content" id="tab2">Контент вкладки 2</div>
        <div class="tab-content" id="tab3">Контент вкладки 3</div>
      </div>
    </div>
  </div>
</main>
body {
  font-size: 16px;
  line-height: 1.5;
  font-family: monospace;
}

header {
  background-color: #f1f1f1;
  margin-bottom: 25px;
  padding: 15px;
  -webkit-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  -moz-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}

header h2.title {
  padding-bottom: 15px;
  border-bottom: 1px solid #999;
}

header p.description {
  font-style: italic;
  color: #222;
}

.result {
  background-color: #f8f8f8;
  padding: 15px;
  -webkit-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  -moz-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}

.tab-headers {
  display: flex;
  margin-bottom: 10px;
}

.tab-header {
  background-color: #ddd;
  border: none;
  padding: 10px 20px;
  cursor: pointer;
}

.tab-header.active {
  background-color: #333;
  color: #fff;
}

.tab-content {
  display: none;
  padding: 15px;
  border: 1px solid #ddd;
}

.tab-content.active {
  display: block;
}
const tabHeaders = document.querySelectorAll('.tab-header');

tabHeaders.forEach(function(header) {
  header.addEventListener('click', function() {
    const targetId = this.dataset.target;
    const targetContent = document.getElementById(targetId);

    // Видаляємо клас "active" з усіх заголовків та контентів
    tabHeaders.forEach(function(header) {
      header.classList.remove('active');
    });
    document.querySelectorAll('.tab-content').forEach(function(content) {
      content.classList.remove('active');
    });

    // Додаємо клас "active" до поточного заголовка та контенту
    this.classList.toggle('active', true);
    targetContent.classList.toggle('active', true);
  });
});

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.