Please note, I understand that below code is far from poetry and I'm beginner. With this in mind, I have following html inside a <form>
:
<div class="image-upload">
<img id="send_photo_img1" src="assets/images/index2.png"/>
<input id="send_photo_input1" type="file"/>
<img id="send_photo_img2" src="assets/images/index2.png"/>
<input id="send_photo_input2" type="file"/>
</div>
Goal: once user clicks image (index2.png, which is a placeholder with "+" sign), window opens like from input=file, user chooses img and it gets loaded instead of the placeholder.
Rest of the code:
Jquery (based on @Ivan Bayev's answer here, while his code it so good, mine got quite clumsy atm..)
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").click(function(){
$("#send_photo_input1").trigger("click");
$("#send_photo_input1").change(function(){
readURL(this);
});
});
CSS
.image-upload > input
{
display: none;
}
It works. But: (1) I feel jQuery is really bad and (2) biggest concern - once image is displayed instead of the placeholder, I still have "loading" sign for like 30-40 seconds. Then it is gone.
Any advise is highly appreaciated. Thanks!