attach listeners to new items

master
Matt Huntington 2 years ago
parent f66c470f35
commit 82933ce565

@ -19,17 +19,24 @@ for(const element of lisAndBody){
})
}
const deleteItem = (event)=>{
event.target.parentNode.remove()
}
const deleteButtons = document.querySelectorAll('button.delete');
for (const deleteButton of deleteButtons) {
deleteButton.addEventListener('click', (event)=>{
event.target.parentNode.remove()
})
deleteButton.addEventListener('click', deleteItem)
}
const createNewItem = (event)=>{
const template = document.querySelector('template').content.children[0];
const newItem = template.cloneNode(true)
newItem.children[1].addEventListener('click',createNewItem);
newItem.children[3].addEventListener('click',deleteItem);
event.target.parentNode.children[0].appendChild(newItem)
}
const addButtons = document.querySelectorAll('button.add');
for (const addButton of addButtons) {
addButton.addEventListener('click', (event)=>{
const template = document.querySelector('template').content.children[0];
event.target.parentNode.children[0].appendChild(template.cloneNode(true))
})
addButton.addEventListener('click',createNewItem);
}

Loading…
Cancel
Save