Objective:
I am aiming to enhance the image upload functionality by enabling users to click on an <img>
element to initiate the file selection process, instead of using the traditional <input type="file">
button. There will be three placeholders initially displayed on the page, and upon clicking on any placeholder, the user can choose a file which will then be displayed in the respective placeholder. Below is what I have implemented so far:
HTML:
<div class="image-upload">
<label for="file_input1">
<img id="send_photo_img1" src="assets/images/index2.png"/&>
</label>
<input id="file_input1" type="file"/>
</div>
CSS:
.image-upload > input
{
display: none;
}
.image-upload img
{
display: inline-block;
margin-right:6px;
width: 90px;
height: 90px;
cursor: pointer;
}
Jquery:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#send_photo_img1')
.attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#send_photo_img1").change(function(){
readURL("#file_input1");
});
While libraries like plupload.js
or dropzone.js
offer similar functionalities, they come with additional features that are unnecessary for my current project requirements. Therefore, I am customizing the implementation to fit precisely what I need.
The existing code allows me to select an image, but it fails to display it. Currently, after selecting an image, a loading icon appears briefly, but no further action takes place. Additionally, there are no error messages displayed in the Chrome DevTools console.