Categories
JavaScript Answers

How to add or remove HTML inside div using JavaScript?

Spread the love

Sometimes, we want to add or remove HTML inside div using JavaScript.

In this article, we’ll look at how to add or remove HTML inside div using JavaScript.

How to add or remove HTML inside div using JavaScript?

To add or remove HTML inside div using JavaScript, we can use the insertAdjacentHTML method.

For instance, we write

const addRow = () => {
  const div = document.getElementById("content");
  div.insertAdjacentHTML("afterbegin", "PUT_HTML_HERE");
};

to define the addRow function.

In it, we call getElementById to get the element with ID content.

Then we call div.insertAdjacentHTML to put HTML at the 'afterbegin' position.

To add HTML after the div.

Conclusion

To add or remove HTML inside div using JavaScript, we can use the insertAdjacentHTML method.

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 *