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

149
Views
Pass index to be accessible in out side function addEventListener

I have a simple function, and need use the index from foreachloop, example:

window.onload = function()
{   
    Array.from(document.getElementsByClassName('image_add_product')).forEach(function(element,index)
    {
        element.addEventListener("change", input_change(index), false);
    });
};

And in input_change function i need the index

function input_change(index)
{
    alert(index);
    alert(this.type);
}

I dont know why, when window load, the change is automatic trigger! Even not change anything. See below:

window.onload = function()
{   
    Array.from(document.getElementsByClassName('image_add_product')).forEach(function(element,index)
    {
        element.addEventListener("change", input_change(index), false);
    });
};

function input_change(index)
{   
    alert(this.type);
    alert(index);
}
<input type="file" class="image_add_product">one
<input type="file" class="image_add_product">two
<input type="file" class="image_add_product">tree

Whats happens?

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!