I have a Bootstrap Modal for uploading multiple images. When the User Click 'Add-Product-Image' Button, it will generate a new image upload input-form, and when the user selects an image it will instantly show an image below the upload button. My code works fine but I have a little issue.
My issue: When the User Click 'Add-Product-Image' Button, it will generate a new image upload input-form then select the image and new image replace the first image. I want to show the multiple images
<div class="product-image">
<div class="form-group">
<div class="Image-upload">
<span class="image-option-close">×</span>
<label for="product_images"> Upload Image </label>
<img src="" id="image" class="h-100" alt="">
<input type="file" id="product_images[]" accept="image/*" name="product_images" onchange="readURL(this);">
</div>
</div>
</div>
$(function () {
$(this).on('click', '#add-product-image', function () {
var form = '<div class="form-group"><div class="Image-upload"><span class="image-option-close">×</span><label for="product_images" class="">Upload Image</label><input type="file" id="product_images" accept="image/*" name="product_images[]" onchange="readURL(this);"></div></div>';
$('.product-image').append(form)
});
$(this).on('click', '.image-option-close', function () {
var target_input = $(this).parent();
target_input.remove();
})
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#image')
.attr('src', e.target.result)
};
reader.readAsDataURL(input.files[0]);
}
}
Please See the gif file https://i.postimg.cc/MG3njJBS/ezgif-com-gif-maker.gif