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

150
Views
Multiple file type inputs with same id not working JS

I have multiple inputs on the same page with type "file" but only the first one is working. I have a "preview" img where the uploaded file will appear. I do not want to use unique IDs and I'm not sure how to use classes if that's the problem. I also tried dynamically creating the divs with JS but no improvement. Codepen below. My goal is to get all the inputs to work. https://codepen.io/pfbarnet/pen/VwMgmbP

 var fileTag = document.querySelector("#filetag"),
        preview = document.querySelector("#preview");
    
          if (fileTag) {
          
            fileTag.addEventListener("change", function () {
              changeImage(this);
            });
          }
        
          function changeImage(input) {
            var reader;
        
            if (input.files && input.files[0]) {
              reader = new FileReader();
        
              reader.onload = function (e) {
                preview.setAttribute("src", e.target.result);
              };
        
              reader.readAsDataURL(input.files[0]);
            }
          }

<input type='file' id= 'filetag' class='file-chooser'><img src='' id='preview'/></input>

<input type='file' id= 'filetag' class='file-chooser'><img src='' id='preview'/></input>

<input type='file' id= 'filetag' class='file-chooser'><img src='' id='preview'/></input>
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!