I am creating add remove image upload. When I add 1 image's file, it will automate create new input file property without button clicked (only click on input property).
When I click del icon to delete the image's file, the image's preview has been removed but input property belong the image still exist, and the result all image’s file post by submit include the delete’s image.
Could you help me to modify this javascript so when I delete the image, it also delete the input property belong it's image, please?
var abc = 0;
$(document).ready(function() {
$('body').on('change', '#file', function(){
if (this.files && this.files[0]) {
abc += 1;
$(this).before("<div id='abcd"+ abc +"' class='abcd'><img id='previewimg" + abc + "' src=''/></div>");
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
// $(this).hide();
$("#abcd"+ abc).append($("<img/>", {id: 'x', src: 'x.png', alt: 'delete'}).click(function() {
$(this).parent().remove();
}));
}
$(this).after($("<div/>", {id: 'filediv'}).fadeIn('slow').append(
$("<input/>", {name: 'file[]', type: 'file', id: 'file'})
));
});
//To preview image
function imageIsLoaded(e) {
$('#previewimg' + abc).attr('src', e.target.result);
};
});
#file{
color:green;
padding:5px; border:1px dashed #123456;
background-color: #f9ffe5;
}
#x{
width: 17px;
height: 17px;
border: none;
margin-left: -20px;
margin-top: 1px;
cursor: pointer;
position: absolute;
}
.abcd img{
height:100px;
width:100px;
padding: 5px;
border: 1px solid rgb(232, 222, 189);
}
<form enctype="multipart/form-data" action="" method="post">
<div id="filediv">
<input name="file[]" type="file" id="file"/>
</div><br/>
<input type="submit" value="Upload File" name="submit"/>
</form>