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

139
Views
¿Cómo obtener src de una imagen antes de que se cambie para usarla más tarde?

Estoy tratando de hacer una vista previa del archivo y tuve un problema al crear una función para eliminar la imagen cargada. Necesito crear una función que elimine la imagen cargada y muestre la imagen predeterminada que tengo. Supongamos que tengo una cuenta con una imagen y luego quiero cambiarla, pero cuando subí un archivo no me gustó y quiero hacer clic en un botón que eliminará esta nueva imagen y regresará a la primera. (digamos que no lo guardé). Entonces, en todos los casos, necesito almacenar el primer src en una variable y luego usarlo más tarde si es necesario. Reproduciré aquí el código que creé para obtener una vista previa del archivo (funciona bien). Ahora necesito ayuda para crear la función de eliminación siguiendo los requisitos que dije antes. Si es posible, sin jquery .

 const inpFile = document.getElementById("inpFile"); const previewContent = document.getElementById("imagePreview"); const previewDefaultText = previewContainer.querySelector(".previewText"); const previewImage = previewContent.querySelector(".previewImage"); inpFile.addEventListener("change", function(){ const file = this.files[0]; if(file){ const reader = new FileReader(); previewDefaultText.style.display = "none"; previewImage.style.display = "block"; reader.addEventListener("load", function() { previewImage.setAttribute("src", this.result); }); reader.readAsDataURL(file); } })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Guarde la fuente original en una propiedad de conjunto de dataset .

 inpFile.addEventListener("change", function() { const file = this.files[0]; if (!previewImage.dataset.origSrc) { previewImage.dataset.origSrc = previewImage.src; } if (file) { const reader = new FileReader(); previewDefaultText.style.display = "none"; previewImage.style.display = "block"; reader.addEventListener("load", function() { previewImage.setAttribute("src", this.result); }); reader.readAsDataURL(file); } }) removeButton.addEventListener("click", function() { if (previewImage.dataset.origSrc) { previewImage.src = previewImage.dataset.origSrc; previewImage.dataset.origSrc = ''; } });

about 4 years ago · Juan Pablo Isaza Report
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!