I'm trying to make the image as a input file button. Once image selected then the default image change to the image selected.
By using the fileReader I am only able to target one image.
- How could I make it only target the
imageattr in that selecteddiv? - Is that possible ?
HTML Code
<div class="prep">
<div class="row">
<label>Step 1</label>
<img src="http://png-4.findicons.com/files/icons/129/soft_scraps/256/button_upload_01.png" id="upfile1" style="cursor:pointer" class="img"/ >
<input type="file" class="inputimg" />
</div>
</div>
<span class="add">Add Step</span>
JS Code.
$(document).on("click" ,".img" , function(){
$(this).closest("div").find(".inputimg").trigger("click");
});
var count = 1;
$(".add").on("click",function(){
count ++;
var row = '<div class="row"><label>Step '+count+'</label><img src="http://png-4.findicons.com/files/icons/129/soft_scraps/256/button_upload_01.png" id="upfile1" style="cursor:pointer" class="img"/ ><input type="file" class="inputimg" />';
$(".prep").append(row);
});
$(".inputimg").change(function(){
readURL(this);
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.img').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
refer to my jsfiddle Demo
- Why the dynamically added image , the alignment is not same with the first one ?
Thank You
labeltag and before theimgtag resolves the issue. I've updated your fiddle.