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

235
Vistas
How can I loop through a string array and only get the value of the data attribute that is clicked on

I am trying to get from the <ul class="click-to-section"> to the tester class where I want to loop over the children that has data-section and only show it if it matches up with the <ul class="click-to-section">

See the screenshot of the HTML from Google Dev tools - https://ibb.co/Y3g1jmC

my code to show this is below:

$(".click-to-section li").click(function() {
    $(this).each(function(){
    let testdata = $(this).data('section');
                            
   let testdata2 = $(this).closest("main").next().next().children();
   $(testdata2).each(function(){
       const dataSection = $(this).data("section");
       console.log(dataSection);
   });
 });  
});

HTML

<ul class="click-to-section">
        <li data-section="videos">Videos</li> **When this is clicked**
        <li data-section="lo">Learning objectives</li>
        <li data-section="credits">Credit</li>
        <li data-section="toolkits">Toolkit</li>
    </ul>

<div class="opinions"></div>
<div class="tester"> **Needs to traverse to this div and loop through the children to show the sections one at a time based on the data-section in the ul menu to equal the data-section here **
    <div data-section="credits" class="js-tabs" style="display: none;"</div>
    <section data-section="videos" class="js-tabs"></section>
    <div class="js-tabs" data-section="lo" style="display: none;"></div>
    <div data-section="toolkits" class="js-tabs" style="display: none;"></div>
</div>

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

0

Updated the code to click on li and show tester elements that matches data attributes of clicked li

Working Code below

$(".click-to-section li").on("click", function() {
  var dataSection = $(this).attr("data-section");
  $(".tester > *").hide();
  $(".tester [data-section=" + dataSection + "]").show();
})
li {
  cursor: pointer
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="click-to-section">
  <li data-section="videos">Videos</li> **When this is clicked**
  <li data-section="lo">Learning objectives</li>
  <li data-section="credits">Credit</li>
  <li data-section="toolkits">Toolkit</li>
</ul>

<div class="opinions"></div>
<div class="tester">
  <div data-section="credits" class="js-tabs">cred </div>
  <section data-section="videos" class="js-tabs">video</section>
  <div class="js-tabs" data-section="lo">lo</div>
  <div data-section="toolkits" class="js-tabs">js tabs</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