I want to remove an appended element as well as its corresponding input file from a document.
I'm creating a file upload system whereby users can select images and preview them before uploading. I have a cancel button which they can use to remove an image they don't want to upload, so far the images are previewing but the cancel button is not working.
Here is my code
$(function() {
var newInput;
$(":file").change(function() {
if (this.files && this.files[0]) {
for (var i = 0; i < this.files.length; i++) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[i]);
}
//CLONE THE INPUT FIELD FOR UPLOADING
newInput = $(this).clone();
newInput = newInput.addClass(this.value);
newInput.appendTo(".thumbimg");
}
});
});
//PREVIEW IMAGE
function imageIsLoaded(e) {
$('.appendImg').append('<img class="myImg" src=' + e.target.result + '><i class="fa fa-circle"></i>');
};
//REMOVE IMAGE AND CLONED INPUT
$(".fa-circle").on('click', function() {
$(this).remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<input type="file" name="file[]" class="addimage" id="addimage" accept="image/*" multiple>
<form enctype="multipart/form-data" method="POST">
<div class="thumbimg">
<input type="submit" class="submit">
</div>
</form>
<div class="appendImg"></div>