document.addEventListener('DOMContentLoaded', function() {
let fileInput = document.getElementById("fileInput")
let displayArea = document.getElementById("displayArea")
fileInput.addEventListener("change", (e) => {
let file = fileInput.files[0];
let reader = new FileReader();
reader.onload = (e) => {
displayArea.innerText = e.target.result;
};
reader.onerror = (e) => {
console.error("File could not be read! Code " + e.target.error.code);
};
reader.readAsText(file);
console.log(displayArea.innerText);
});
}, false);
<input type="file" id="fileInput">
<div id="displayArea"></div>
div
contains the text, but console.log
gets <empty string>
.
I think it logs before reader.onload
.
I tried to do something with async
and await
, but without any success.
Thanks a lot!