0

I'm making a 2do app as a personal project. I've gotten the code to delete the lis and add new lis, but The new lis will not remove themselves when I click on the 'x' span.

// delete done todos
for (var i = 0; i < deleteTodo.length; i++) {
  deleteTodo[i].addEventListener('click', function(e) {
    this.parentNode.remove();
  });
}
// add a todo task
input.addEventListener('keypress', function(e) {
  if (event.keyCode === 13) {
    var newTodo = input.value;
    var newLi = document.createElement('li');
    newLi.innerHTML = '<span>X</span> ' + newTodo;
    this.value = '';
    console.log(newLi);
    ul.appendChild(newLi);
  }
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236

1 Answers1

0

You want to delegate the event.

document.addEventListener('click', (event)=>{
    if(event.target.matches('li')){
        // this will fire on any li tags that are clicked, even if dynamically added
    }
})
Gavin
  • 2,214
  • 2
  • 18
  • 26