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

256
Vistas
Jquery alert data attribute of element with style attribute

i have a short question:

I have multiple elements with the same class, and i want to alert dynamically the data-index value of the the element with the style "display: block".

Here is my code:

var test = $('.test').data('index');

alert( test );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="test" style="display:none" data-index="0" href="#">foo!</a>

<a class="test" style="display:block" data-index="1" href="#">foo!</a>

<a class="test" style="display:none" data-index="2" href="#">foo!</a>

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

0

:visible can be used just like in CSS for this purpose

var test = $('.test:visible').data('index');

alert( test );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="test" style="display:none" data-index="0" href="#">foo!</a>

<a class="test" style="display:block" data-index="1" href="#">foo!</a>

<a class="test" style="display:none" data-index="2" href="#">foo!</a>

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