How to fix setting innerHTML not updating the DOM with JavaScript?

Sometimes, we want to fix setting innerHTML not updating the DOM with JavaScript.

In this article, we’ll look at how to fix setting innerHTML not updating the DOM with JavaScript.

How to fix setting innerHTML not updating the DOM with JavaScript?

To fix setting innerHTML not updating the DOM with JavaScript, we should set the innerHTML property of the element to an HTML string.

For instance, we write:

<div>

</div>

to add a div.

Then we write:

const div = document.querySelector('div')
div.innerHTML = '<b>foo</b>'

to select the div with querySelector.

Then we set div.innerHTML to a string with the elements we want to put in the div.

Conclusion

To fix setting innerHTML not updating the DOM with JavaScript, we should set the innerHTML property of the element to an HTML string.