Categories
JavaScript Answers

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

Spread the love

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.

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 *