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

282
Views
How can i delete images preview each time file input is clicked?

I'm trying to reset the image preview created by file input each time the upload button clicks. I'm new to JS so forgive me if I missed the obvious.

This is a code snippet I found to show the preview:

$(function() {
var imagesPreview = function(input, placeToInsertImagePreview) {
if (input.files) {
var filesAmount = input.files.length;
for (i = 0; i < filesAmount; i++) {
var reader = new FileReader();
reader.onload = function(event) {
$($.parseHTML('<img class="padit">')).attr('src', event.target.result).appendTo(placeToInsertImagePreview);
}
reader.readAsDataURL(input.files[i]);
}
}
};
$('#gallery-photo-add').on('change', function() {
imagesPreview(this, 'div.gallery');
});
});

This is my HTML:

<div class="upload-field">
        <label class="bcard-label" for="gallery-photo-add">
        <input id="gallery-photo-add" type="file" accept="image/*" value="" multiple>
          <i class="fa fa-2x fa-camera"></i>
          </label>
      </div>
      <div class="gallery">

      </div>

This is the CSS:

.upload-field{
  display: flex;
  justify-content: center;
  align-items: center;
  max-width: 95%;
  border: 4px dashed grey;
  height: 100px;
  margin: auto;
}

.gallery{
  display: inline-block;
  column-count: 2;
  margin: 15px;
}

img.padit{
padding: 5px;
}

And with my very little knowledge in JS I tried to complete the task with this snippet:

const element = document.getElementById("gallery-photo-add");
element.addEventListener("click", function() {
document.getElementById("padit").remove();

Any ideas where am I wrong? Thanks :)

about 4 years ago · Santiago Gelvez
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!