Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda