Categories
JavaScript Answers

How to append HTML to container element without innerHTML with JavaScript?

Spread the love

To append HTML to container element without innerHTML with JavaScript, we call the insertAdjacentHTML method.

For instance, we write

const d1 = document.getElementById("one");
d1.insertAdjacentHTML("beforeend", '<div id="two">two</div>');

to select the container element with getElementById.

And then we call insertAdjacentHTML method with 'beforeend' and a string with the element to insert.

'beforeend' inserts the HTML as the last child of the container 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 *