textContent property of an
Element is one way to output text on a web page.
For example, consider the following HTML tag:
To change its
document.getElementById("paragraph").textContent = "Hello, World";
This will select the element that with the id
paragraph and set its text content to "Hello, World":
<p id="paragraph">Hello, World</p>
<body> <h1>Adding an element</h1> </body>
<p> tag with a
textContent property of and add it at the end of the html body:
var element = document.createElement('p'); element.textContent = "Hello, World"; document.body.appendChild(element); //add the newly created element to the DOM
That will change your HTML body to the following:
<body> <h1>Adding an element</h1> <p>Hello, World</p> </body>
<script> tags after all of your other
<body> content. Alternatively, you can also use an event listener to listen to eg.
onload event, adding your code to that event listener will delay running your code until after the whole content on your page has been loaded.