How to Change Text After Page Load With Javascript

Sometimes we have to change our website content or text dynamically instantly after page load. At that time, JavaScript comes in the frame to help us.

It is effortless to change text content in our HTML webpage after page load.

Change text with innerHTML

There is a simple way to change the text of any element.

We have to use the innerHTML property of our selected element to change the text.

Syntax

document.getElementById(‘id_name’).innerHTML = “new_text”;

Example 1

<!DOCTYPE html>
<html>
<title>How to change text on page load</title>
<body onload="change_text()">
<p id="demo">Errorsea.com is the best website for web designing</p>
<script>
function change_text(){
document.getElementById("demo").innerHTML = "Errorsea.com is the best platform to understand basic consepts of website designing";
}
</script>
</body>
</html>

Explanation

  • First, we create a change_text() function to change the text of the demo element.
  • After that, we set an onload event for change_text() function in <body> tag.

Read Also: How to Change Text OnClick Event JavaScript

Example 2

If we can not set an onload event at <body> tag, we can also use window.onload to set an event for our function in JavaScript.

<!DOCTYPE html>
<html>
<title>How to change text on page load</title>
<body>
<p id="demo">Errorsea.com is the best website for web development</p>
<script>
function change_text(){
document.getElementById("demo").innerHTML = "Errorsea.com is the best platform to understand basic consepts of website development";
}
window.onload=change_text;

</script>
</body>
</html>

Conclusion

I am sure you have no dought about adding text using JavaScript after page load.

Enjoy Errorsea 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *