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

223
Visualizações
reupload the type=file once uploaded till submit

I am making a form where I need to uplaod images where I can change the uploaded image until I submit the form

function previewImage() {
  var thisElement = event.target
  var file = thisElement.files;
  var fileReader = new FileReader();
  fileReader.onload = function(event) {
    thisElement.nextElementSibling.setAttribute("src", event.target.result);
  }
  thisElement.style.display = 'none';
  fileReader.readAsDataURL(file[0])
};
<input type="file" id="file1" accept="image/*" onchange="previewImage();">
<img id="display1"><br>
<input type="file" id="file2" accept="image/*" onchange="previewImage();">
<img id="display2"><br>
<input type="file" id="file3" accept="image/*" onchange="previewImage();">
<img id="display3"><br>
<input type="file" id="file4" accept="image/*" onchange="previewImage();">
<img id="display4"><br>

I want to change the uploaded file but I can't do so once I uploaded the file.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Just remove this line that is hiding your file input once a file has been selected :

thisElement.style.display = 'none';
about 4 years ago · Juan Pablo Isaza Relatório

0

I would do this by:

  • Wrap each image in a <label> for the relevant input
  • Update your image loaded event to target the image specifically using nextElementSibling.querySelector("img")
  • Wrap the fileReader.readAsDataURL(file[0]) in an if to avoid an error when the user cancels file selection.

That way you can click the image to change it after the first time it is set.

function previewImage() {
  var thisElement = event.target
  var file = thisElement.files;
  var fileReader = new FileReader();
  fileReader.onload = function(event) {
    thisElement.nextElementSibling.querySelector("img").setAttribute("src", event.target.result);
  }
  thisElement.style.display = 'none';
  if (file.length) {
    fileReader.readAsDataURL(file[0])
  }
};
<input type="file" id="file1" accept="image/*" onchange="previewImage();">
<label for="file1"><img id="display1"></label><br>
<input type="file" id="file2" accept="image/*" onchange="previewImage();">
<label for="file2"><img id="display2"></label><br>
<input type="file" id="file3" accept="image/*" onchange="previewImage();">
<label for="file3"><img id="display3"></label><br>
<input type="file" id="file4" accept="image/*" onchange="previewImage();">
<label for="file4"><img id="display4"></label><br>

about 4 years ago · Juan Pablo Isaza Relatório
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