I'm trying to Figure out how to replace HTML text using js code, the goal of it is the read a "1.txt" file with a name and replace the HTML text (Replace me!!) if anyone can help me and point me in the right direction.
here is what I have now
<body class="htmlNoPages">
<div id="main"><p>Replace me!!</p></div>
</body>
<script type="text/javascript" id="gwd-init-code">
function load() {
// var text = ('test 123')
var file = new XMLHttpRequest();
file.open(GET,'file:///d:/1.txt', true);
file.responseType =Text;
file.onreadystatechange = function() {
if (file.readyState === 4) { // Makes sure the document is ready to parse
if (file.status === 200) { // Makes sure it's found the file
text = file.responseText;
}
}
}
let element = document.querySelector('#main');
element.innerHTML = text;
}
window.onLoad = load();
</script>
I did it like this
var TxtFile = new XMLHttpRequest();
TxtFile.onreadystatechange = function () {
var allText = "Uw browser is niet compatibel of u heeft uw java script disabeld staan";
if (TxtFile.readyState === XMLHttpRequest.DONE && TxtFile.status == 200) {
allText = TxtFile.responseText;
allText = allText.split("\n").join("<br>");
}
document.getElementById('planning').innerHTML = allText;
}
TxtFile.open("GET", 'File location', true);
TxtFile.send(null);