This is a madeup example of the actual situation. For my purpose i need to return a value assigned to a variable which is initialized inside onload event handler. It is returning undefined. Is there any way to return the variable ?
in this case, i need to return the element variable from usefile function.How i can manage that.
<html>
<body>
<input type='file' id='up' accept='image/*' >
<script>
function fileread(e){
if(window.FileReader && window.File && window.FileList){
var file=e.target.files[0];
var getElement=usefile(file);
console.log(getElement); // prints 'undefined'
}
}
function usefile(file){
var reader=new FileReader();
reader.onload=function(e){
var element=getRandomElement();
return element; // need to return it and assign it to getElement variable inside fileread function
}
}
function getRandomElement(){
var arr=[1,2,3];
return arr[2]; // a random value for experiment purpose
}
document.getElementById('up').addEventListener('change',fileread,false);
</script>
</body>
</html>