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

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.