Trying to upload an image file with preview using Jquery. It's working and I got the preview, but when I click on the upload button I saw an error message
Uncaught RangeError: Maximum call stack size exceeded
in the console.
Here is the code:
$(document).ready(function() {
var readURL = function(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.profile-image').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$(".file-upload").on('change', function() {
readURL(this);
});
$(".upload-button").on('click', function() {
$(".file-upload").click();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<img class="profile-image" src="demo.png" alt="Profile Image">
<a class="profile-img-edit upload-button">
<i class="fas fa-camera"></i> Select Image
<input class="file-upload" type="file" name="file" size="40" accept=".png, .jpg, .jpeg, .gif">
</a>
</div>