Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
How to select the content from the element that I clicked on (using data attributes)

I am working with HTML and JS and I am trying to add a click event listener so that the index = 0 value updates automatically on click.

Depending on which .tab element I click, the content inside of the div.tab should change to display either Hello1, Hello2 or Hello3. The first tab should display the tab content hello1 accordingly.

I have provided both with data attributes in the HTML. I want the corresponding div.tab content to be output if both have the same data attribute with vanilla JS.

JS

const tabbarContent = document.querySelectorAll(".tab-content");
const tabbarButtons = document.querySelectorAll(".tab");
const index = 0;
tabbarContent[index].classList.add('active');
tabbarButtons[index].classList.add('active');
const dataAttribute1 = tabbarButtons[index].getAttribute("data-tab");
const dataAttribute2 = tabbarContent[index].getAttribute("data-tab");



function clickTab() {
  for (let index = 0; index.length; index++) {
    if (dataAttribute1[index] === dataAttribute2[index]) {
      dataAttribute2.classList.add("active");
    }
  }
}

tabbarButtons[index].addEventListener("click", clickTab);

HTML

<div class="container">
  <div class="inner-container">
    <div class="tab tab1" data-tab="1">
      1
    </div>
    <div class="tab tab2" data-tab="2">
      2
    </div>
    <div class="tab tab3" data-tab="3">
      3
    </div>
  </div>
  <div class="inner-container">
    <div class="tab-content content1" data-tab="1">
      <h1>Hello1</h1>
    </div>
    <div class="tab-content content2" data-tab="2">
      <h1>Hello2</h1>
    </div>
    <div class="tab-content content3" data-tab="3">
      <h1>Hello3</h1>
    </div>
  </div>
</div>

Thanks Jessy

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If I understand correctly what you want to achieve, you could do something like:

const tabbarButtons = document.querySelectorAll(".tab");
const tabbarContents = document.querySelectorAll(".tab-content");

tabbarButtons.forEach((tabBtn) => {
  tabBtn.addEventListener('click', () => {
    tabbarContents.forEach(tabCnt => {
      if (tabCnt.dataset.tab === tabBtn.dataset.tab) {
        tabCnt.classList.add('active');
      } else {
        tabCnt.classList.remove('active');
      }
    })
  });
})
.tab {
  display: inline-block;
  padding: 5px;
  border: 1px solid #ccc;
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}
<div class="container">
  <div class="inner-container">
    <div class="tab tab1" data-tab="1">
      1
    </div>
    <div class="tab tab2" data-tab="2">
      2
    </div>
    <div class="tab tab3" data-tab="3">
      3
    </div>
  </div>
  <div class="inner-container">
    <div class="tab-content content1" data-tab="1">
      <h1>Hello1</h1>
    </div>
    <div class="tab-content content2" data-tab="2">
      <h1>Hello2</h1>
    </div>
    <div class="tab-content content3" data-tab="3">
      <h1>Hello3</h1>
    </div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda