-1

How do I pass one variable on the function previewImages?

document.querySelector('#file-input0').addEventListener("change", previewImages,false);
document.querySelector('#file-input1').addEventListener("change", previewImages,false);

I want this script to call the function:

function previewImages(i){
        var preview = document.getElementById("preview"+i);
        if (this.files) {
          [].forEach.call(this.files, readAndPreview);
        }}

Everytime I tried this I cant achieve passing the variable value. I want to implement this in pure javascript.

Erlisar Vasquez
  • 460
  • 3
  • 13

1 Answers1

2

You can declare a function directly as a handler and call the function previewImages from inside of it.

document.querySelector('#file-input0').addEventListener("change", function() {
  previewImages(0);
}, false);

document.querySelector('#file-input1').addEventListener("change", function() {
  previewImages(1);
}, false);
Ele
  • 33,468
  • 7
  • 37
  • 75