I make a button to dynamically create input field. After uploading file the image should be previewed . But find that , at first one image is previewed , then 2nd time after selecting file 2 copy of same image is previewed, then 3rd time 3 copy of same image and goes on…
Html code
<form method="post" action=" " class="form" id="form" accept-charset="UTF-8"
enctype="multipart/form-data" >
<div class="field" align="left">
<h3>Upload your images</h3>
<input type="text" placeholder="Event name">
<input type="file" id="1" class="gallery_input" name="files[]" multiple />
</div>
Js code
<script type="text/javascript">
var event_id=3;
var input_id;
$(document).ready(function ()
{
function readURL(input)
{
if(input.files && input.files[0])
{
var reader=new FileReader();
reader.onload = (function(e) {
var file = e.target;
$("<span class=\"pip\">" +
"<img class=\"imageThumb\" src=\"" + e.target.result + "\" title=\"" + file.name + "\"/>" +
"<br/><span class=\"remove\">Remove image</span>" +
"</span>").insertAfter("#"+input_id);
$(".remove").click(function(){
$(this).parent(".pip").remove();
});
});
reader.readAsDataURL(input.files[0]);
}
}
$("body").click(function()
{
$(".gallery_input").change(function()
{
var target_input=$(this).attr('id');
input_id=target_input;
readURL(this);
});
});
});
</script>
If I remove
$("body").click(function()
line it’s work . But I need this because it work’s with dynamically created
input filed .
I remove some HTML and js code like input filed create Js code, etc. Because I think it works.