I have a .txt
file called sample.txt
, where I want to load some text and display it into my HTML document.
Here is my code:
<div id="test"></div>
document.getElementById('test').addEventListener("load", getText);
console.log("script loaded succesfully")
function getText(){
fetch('sample.txt').then(function(data){
console.log("hello")
console.log(data.text);
});
}
My issue is that the function getText
, is not called on load, how can this be, when it logs out script loaded successfully?