I write a very simple Todo app with JS. It's work fine, I have some default tasks that I can click on them to change them to done or undone, but the problem is when I add a new task, I can't do anything on it.
const input = document.querySelector('#todoText');
const todo = document.querySelector('#todo');
const todoLi = document.querySelectorAll('#todo li');
// Add Todo
input.addEventListener('keyup', e => {
if (e.keyCode === 13) {
// Get input value
let val = e.target.value;
// Create <li>
const li = document.createElement('li');
// Create text node from input value
let text = document.createTextNode(val);
// Pass input value into <li>
li.appendChild(text);
// Add input value in Todo list
todo.appendChild(li);
// Reset input value after enter
e.target.value = '';
}
})
todoLi.forEach( item => {
item.addEventListener('click', e => {
if ( e.target.classList.contains('done') ) {
e.target.classList.remove('done')
} else {
e.target.classList.add('done')
}
})
})
This is my PEN on Codepen