I am trying to create a Todo list and wonder how I would go about keeping the completed todos from vanishing each time I add a new task. I am aware that this is happening because I clear my div each time a new task is added, however I am unsure on how to proceed for me to keep using arrays on this and still show completed tasks along with new tasks.
Codepen: https://codepen.io/martinkariuki7-the-looper/pen/OJvQXRW
const newToDo = document.getElementById('todo-new')
const addBtn = document.getElementById('addToDo')
const main= document.getElementById('toDoList')
const taskDone = document.getElementsByClassName('task')
let toDoList = []
// populate to do list
function populateToDo(){
let todo = newToDo.value
todo !== null && todo !== '' ? toDoList.push(todo) : alert('You must write something')
updateDOM()
}
//Update DOM
function updateDOM(){
// Clear main div
main.innerHTML = `<h1>To do list </h1>`
// Show tasks on the front
toDoList.forEach(item =>{
let task = document.createElement('div')
task.classList.add('task')
task.innerHTML = `<label><input type="checkbox" >${item}</label><br>`
main.append(task)
task.addEventListener('change', () => task.classList.toggle('task-complete'))
newToDo.value = ''
})
}
// Event listeners
// Add Tasks
addBtn.addEventListener('click', populateToDo)
newToDo.addEventListener('keydown', (e) => {
if(e.code === 'Enter'){
e.preventDefault()
populateToDo()
}
})