javascript - Reading multiple image using fileReader -
javascript - Reading multiple image using fileReader -
i'm trying create image input file button. 1 time image selected default image alter image selected. using filereader
able target 1 image.
image
attr in selected div
? is 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 jsfiddle demo
why dynamically added image , alignment not same first 1 ?thank you
yes possible have traverse dom in proper hierarchy.
you need create 2 changes.
bind alter event newly dynamically created element too. can achive adding next code after $(".prep").append(row);
statement
$(".inputimg").change(function () { readurl(this); });
in realurl()
function have more specific selecting $('.img')
select image tags , not 1 near clicked object. part can changed $(input).siblings('.img').attr('src', e.target.result);
have @ updated jsfiddle
update 1:
for lastly question i.e. misalignment of images, mentioned @rageit in comment need add together space in between <label>
, <img>
tag in dynamically created elements, updated fiddle reflects that.
javascript jquery html
Comments
Post a Comment