Categories
JavaScript Answers

How to fix an element not updating when the innerHTML property is set with JavaScript?

Spread the love

Sometimes, we want to fix an element not updating when the innerHTML property is set with JavaScript.

In this article, we’ll look at how to fix an element not updating when the innerHTML property is set with JavaScript.

How to fix an element not updating when the innerHTML property is set with JavaScript?

To fix an element not updating when the innerHTML property is set with JavaScript, we should make sure the element is loded before we try to set its innerHTML value.

For instance, we write:

<h1 id="ma">s</h1>

to add an h1 element.

Then we write:

window.onload = () => {
  document.getElementById("ma").innerHTML = "JavaScript";
}

to set the window.onload property to a function that selects the h1 element and set the innerHTML property to 'JavaScript'.

window.onload functions when the page is loaded.

Conclusion

To fix an element not updating when the innerHTML property is set with JavaScript, we should make sure the element is loded before we try to set its innerHTML value.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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