Categories
JavaScript Answers

How to insert or remove HTML content between div tags with JavaScript?

Spread the love

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.

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 *