Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda