Check out this Fiddle I created! It seems to be working fine with Bootstrap 3, but not with Bootstrap 4. I suspect it might have something to do with the hidden class in Bootstrap 4. Any suggestions on how to modify the JS code to make it compatible?
JS:
$(document).ready(function() {
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
var
$container = $(input).closest('.upload'),
$preview = $container.find('.img-preview'),
$uploadedImage = $container.find('.uploaded-image'),
$addImage = $container.find('.add-image');
reader.onload = function(e) {
$preview.attr('src', e.target.result);
if ($uploadedImage.is(':hidden')) {
$uploadedImage.toggleClass("hidden")
$addImage.toggleClass("hidden")
}
}
reader.readAsDataURL(input.files[0]);
}
}
$(".imgInp").change(function() {
readURL(this);
});
});
$('#delete2').on('click', function() {
$('#image2').val("")
$('.deleteme2a').removeClass("hidden")
$('.deleteme2b').addClass("hidden")
$("#hidden-image2").val("change");
});
// Repeat delete functions for image 3, 4 and 5
HTML:
<div class="col-md-4 col-lg-2 ">
<p style="text-align:center;"> <b> Image 5</b>
</p>
<div class="upload center-block">
<span style="background: #black;float:right; position:absolute;cursor:pointer; left: 145px; z-index: 10; padding: 6px 7px;" class="badge " id="delete5"> <i class="fa fa-remove"></i> </span>
<input class="input-file imgInp" id="image5" name="image5" type="file">
<label for="files">
<span class="add-image deleteme5a ">
Add Image
<br>Image</span>
<span class="uploaded-image deleteme5b hidden ">
<img class="img-preview" src="" width="160" height="160" alt="your image" style="margin:0px">
</span>
<output id="list"></output>
</label>
</div>
</div>