I have a problem when appending new element with JavaScript. I don't use any library such as jQuery or anything else, i'm using native JavaScript while doing this.
Okay, i'm trying to append a new element to the <body>
tag. If in jQuery you can use the .append()
function, but when using native JavaScript you must use .innerHTML
.
Here's my HTML code
<button id="add">
Add
</button>
Here's my JavaScript code
document.getElementById("add").addEventListener("click", (e) => {
document.getElementsByTagName("body")[0].innerHTML += '<p>Hello world</p>';
});
When i click the 'add' button it's works the first time. But, when i click again it doesn't work. When i try to use jQuery to append new element then it works, is there any less?