DOM Manipulation - Adding DOM nodes on button click
by ananyaneogi
HTML
<button id="button">Add Content!</button>
<ul id="list"></ul>
JavaScript
const list = document.getElementById('list');
let count = 0;
const button = document.getElementById('button');
function addListItems(type, content, destination) {
const tag = document.createElement(type);
tag.innerText = `${content} ----- ${count++}`;
destination.appendChild(tag);
}
list.addEventListener('click', (e) => {
console.log(e.target);
});
button.addEventListener('click', () => {
addListItems('li', 'hello world', list);
});