In a todo app I'm making, I want to remove the completed tasks by clicking on a button.
This is what I have in my body
:
<div id="div-todo"></div>
<button id="hide-todo">Hide completed</button>
<script src="script.js"></script>
</body>
And this is my js code:
const todos = [
{
text: 'Learn HTML',
completed: true
},{
text: 'Learn CSS',
completed: true
},{
text: 'Learn Javascript',
completed: false
},{
text: 'Learn Nodejs',
completed: false
},{
text: 'Learn SQL',
completed: false
}
]
// renderring and printing tasks
const renderTodo = function (todos) {
const incompletedTodo = todos.filter(function (todo) {
return !todo.completed
})
//printing the h2 tag that containe the number of incompleted task
const summery = document.createElement('h2')
summery.textContent = `You have ${incompletedTodo.length} todos left. `
document.querySelector('#div-todo').appendChild(summery)
//printting all of tasks
todos.forEach(function (todo) {
const p = document.createElement('p')
p.textContent = todo.text
document.querySelector('#div-todo').appendChild(p)
})
// button setting
document.querySelector('#hide-todo').addEventListener('click',function (e) {
const completedTodo = todos.filter(function (todo) {
return todo.completed
})
e.target.textContent = 'Completed task is hide'
console.log(completedTodo)
})
}
renderTodo(todos)
I want to hide uncompleted task with a button, I even log the completedTodo
variable, but I don't know how to remove them (I am new to JS).