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

175
Views
JS add event handler using for loop, passing self as parameter not working

I am wondering why the statement "let im = _imgs[i];" is indispensable in this situation. Essentially, the _imgs are an array of image elements. But if I add the eventhandlers by _imgs[i].addEventListener("click", function(){clickImage(_imgs[i]);}, false);, it won't work.

Can anyone explain why? Thanks a lot!

function init()
{
    _imgs = document.querySelectorAll("#block2 img");
    console.log(_imgs.length);
    _clicked = new Object();
    for(var i = 0; i < _imgs.length; i++)    
    {      
        let im = _imgs[i];
        _imgs[i].addEventListener("click", function(){clickImage(im);}, false);
        _clicked[_imgs[i].id] = 0;
    }
    console.log("hi");
}
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!