I created a function to clone input files if the user clicked twice on the choose file button to prevent the field from clearing the previous uploaded images
$(function() {
// Multiple images preview with JavaScript
var multiImgPreview = function(input, imgPreviewPlaceholder) {
if (input.files) {
$("#multipleImageUpload").on("change", function(e) {
var filesAmount = input.files.length;
for (i = 0; i < filesAmount; i++) {
var reader = new FileReader();
reader.onload = function(event) {
$($.parseHTML("<span class=\"pip\">" +
"<br/><span id=\"remove_icon\">Remove</span>" +
"<img src=\"" + event.target.result + "\" title=\"" + "\"/>" +
"</span>")).appendTo(imgPreviewPlaceholder);
$("#remove_icon").click(function(){
$(this).parent(".pip").remove();
});
}
reader.readAsDataURL(input.files[i]);
}
});
}
};
$(document).on('click', '#multipleImageUpload', function() {
// add to preview
multiImgPreview(this, 'div.imgPreview');
//clone
var originalInput = $(this), cloned = originalInput.clone();
// move
originalInput.attr("id", Date.now());
originalInput.removeClass('multipleImageUpload');
originalInput.appendTo($('#multipleFilesPH'));
cloned.attr("id", "multipleImageUpload");
cloned.insertAfter($('#multipleFilesPH'));
originalInput = null;
cloned = null;
});
});
the issue here is when the user clicks on remove button it only removes the image from the view and the uploaded file is not removed and is sent to the server. any Idea what I'm doing wrong here?
Remove"` - Ids have to be **unique** – Andreas Feb 14 '22 at 16:16