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?