I have the following html setup and cannot solve this issue.
When a button in the html document gets clicked, an element
<div id='wrapper'>
<input id='fileUpload' name='fileUpload' type='file' onchange='uploadpreview(this.id)' multiple />
<br />
<div id='image-holder'></div>
</div>
gets added using the function
function add(){
$j("<?php echo($divoutput)?>").insertBefore("#addbox");
}
(the php code has the html format in there).
However when this is done, the javascript function (Which is already in the html document and does not get added via a code) does not work:
$("#fileUpload").on('change', uploadpreview() {
//Get count of selected files
var countFiles = upload[0].files.length;
var imgPath = upload[0].value;
var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
var image_holder = $("#image-holder");
image_holder.empty();
if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
if (typeof (FileReader) != "undefined") {
//loop for each file selected for uploaded.
for (var i = 0; i < countFiles; i++) {
var reader = new FileReader();
reader.onload = function (e) {
$("<img />", {
"src": e.target.result,
"class": "thumb-image"
}).appendTo(image_holder);
}
image_holder.show();
reader.readAsDataURL(upload[0].files[i]);
}
} else {
alert("This browser does not support FileReader.");
}
} else {
alert("Pls select only images");
}
}
It throws an error that length cannot be read etc, even though a file has been uploaded.
What can be the trouble of this?
I have tried the code separately (without being added via a code) and everything works flawlessly.
Please help!