3 ms·
Would you not just be able to do the following: const div = document.createElement('div'); const text = 'This is a dynamically generated paragraph';
by makingstuffs 2y ago
Would you not just be able to do the following:
const div = document.createElement('div');
const text = 'This is a dynamically generated paragraph';
div.insertAdjacentHTML("beforeend", `<div id="container" class="my-class">
<h1>Hello world</h1>
<p>${text}</p>
</div>`);
document.body.append(...div.children);
Edit, figured I'd add the docs: https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML https://developer.mozilla.org/en-US/docs/Web/API/Element/ins...
- TimTheTinker 2y agoThat's certainly better than innerHTML, but it has the same security implications as setting innerHTML. From the MDN page: > When inserting HTML into a page by using insertAdjacentHTML(), be careful not to use user input that hasn't been escaped.