Pass value from text file into a html paragraph

59 Views Asked by At

I have a text file populated dynamically from another javascript. Owing to require/bundle issues and many, many other problems I am just writing the results to a text file.

I now want to display the results in a paragraph on the webpage. Accessing the info seems straightforward enough, printing to the console works with the below, but how do I now put the text in a paragraph so I can format it with CSS etc:

The text file contains:

let text = 'Some random text';

And then html file has this in it:

</head>
<body>
    <h1>Wallboard</h1>

<script src=output.txt></script>
<script>
let pageText = text;

console.log(pageText)

let myParagraph = document.querySelector("#pageText");

console.log(myParagraph)

myParagraph.innerHTML = pageText;

;
</script>
<p id="pageText"></p>

</body>
</html>
1

There are 1 best solutions below

6
Jarmo T On BEST ANSWER

You will have to find the HTML element with JavaScript and then set the elements inner HTML to be your text content:

</head>
<body>

<script src=output.txt></script>

<p id="pageText"></p>

<script>
let pageText = "Your text here";

let myParagraph = document.querySelector("#pageText");

console.log(myParagraph)

myParagraph.innerHTML = pageText;

</script>

</body>
</html>