Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Geting data-index of images with same className by using eventListener

Let's assume that I have ten images with different data-index but same class and src such as:

<img src="images/image.png"  class="image" data-index="${index})">

And I want to reach the data-index of clicked image with eventListener

const image= document.getElementsByClassName('image');

image.forEach(el => el.addEventListener('click', function() {
     console.log(el.getAttribute('data-index'));

}));

But, I get the error which says image.forEach is not a function. How can I solve this? How can I get the data-index of clicked image. This task needed to be done with eventListener. It cannot be done with onClick because some other parts are failing otherwise. Also, these images are printed by using innerHTML in JS code.

So basically, I have images with same attributes but only not data-index. How can I reach the data-index of clicked image?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda