Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!