Sometimes, we want to insert or remove HTML content between div tags with JavaScript.
In this article, we’ll look at how to insert or remove HTML content between div tags with JavaScript.
How to insert or remove HTML content between div tags with JavaScript?
To insert or remove HTML content between div tags with JavaScript, we can set the innerHTML
property of the element.
For instance, we write:
<div>
hello world
</div>
to add a div.
Then we write:
const div = document.querySelector('div')
div.innerHTML = '<span>Something</span>';
to select the div with querySelector
.
Then we set div.innerHTML
to a string with a span to replace ‘hello world’ with the span.
Therefore, we should see ‘Something’ instead of ‘hello world’ displayed.
Conclusion
To insert or remove HTML content between div tags with JavaScript, we can set the innerHTML
property of the element.