Hello all,
I’m having trouble opening a text file using a .js file.
The webpage contains several small images which - on a ‘onmouseover’ instruction - are displayed full size.
Each image has its own text file, which I need to display in the div id=fileContents.
Using the following HTML code I can choose the correct text file:
HTML:
<script>
var txtfile = "file0.txt";
</script>
<body>
<div id="fileContents" ></div>
<input type="file" id="tekstfile"><script src="app.js"></script>
</body>
APP.JS contains:
document.getElementById(tekstfile).onchange = function() {
document.getElementById("fileContents").textContent = "";
var file = this.files[0];
var reader = new FileReader();
reader.onload = function(progressEvent) {
var lines = this.result.split('\n');
for(var line = 0; line < lines.length; line++) {
console.log(lines[line]);
if (line > "-1") {
document.getElementById("fileContents").innerText += lines[line];
} else {
document.getElementById("fileContents").textContent = lines[line];
}
}
}
reader.readAsText(file);
}
The var txtfile changes along with every image change. So I would like to use the value of var txtfile to open the text file instead of the input value.