This function should display the image to upload. But on selecting a file it is not showing the image.
function show_img(id_img) {
if(this.files && this.files[0])
{
var reader = new FileReader();
reader.onloadend = function(data) {
var image = document.getElementById(id_img);
image.src = data.target.result;
}
reader.readAsDataURL(this.files[0]);
}
}
function create_img()
{
//create image
var file_input = document.createElement('img');
file_input.id = "123_qwe";
file_input.height = "200";
file_input.width = "300";
contain.appendChild(file_input);
//btn upload image
var btn_image = document.createElement("INPUT");
btn_image.setAttribute("type", "file");
btn_image.name = "ref_img";
var id_image = "ref_img";
btn_image.id = id_image;
contain.appendChild(btn_image);
//Display image
document.getElementById(id_image).addEventListener("onchange", function(){
show_img.call(id_image);
console.log(id_image);
});
}
<div id="contain">
<Button id="btn_top" onclick="create_img();">create image</Button>
</div>
How could I make the image appears in the image tag ? Thank you in advance.