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

160
Views
Pase el índice para que sea accesible en la función externa addEventListener

Tengo una función simple y necesito usar el índice del ciclo foreach , ejemplo:

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

Y en la función input_change necesito el index

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

¡No sé por qué, cuando se carga la ventana, el change se activa automáticamente! Incluso no cambiar nada. Vea abajo:

 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

¿Que pasa?

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!