Skip to content
Advertisement

Insert HTML code to div onClick but does not replace existing code

I would like to insert HTML code to make a “list”. I’ve seen examples of innerHTML but that just replaces the existing code. How can I add more code without replacing the current code?

JavaScript
JavaScript

Advertisement

Answer

Use insertAdjacentHtml. Docs – https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML.

JavaScript

‘beforeEnd’ means it will add right before the end of the element(inside the element).

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement