-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (61 loc) · 1.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const tasks = document.querySelector(".tasks");
const addTask = document.querySelector(".add");
const clear = document.querySelector(".clear");
const message = document.querySelector(".message span");
const search = document.querySelector(".search");
function updateMessage(){
count = tasks.children.length;
message.innerText = `You have ${count} pending tasks.`
}
addTask.addEventListener("submit", event => {
event.preventDefault();
const task = addTask.task.value.trim();
if(task.length){
tasks.innerHTML += `<li>
<span>${task}</span>
<i class="bi bi-trash-fill delete"></i>
</li>`
addTask.reset();
updateMessage();
}
});
tasks.addEventListener("click", event => {
if (event.target.className.includes("delete")){
event.target.parentElement.remove();
updateMessage();
}
});
clear.addEventListener("click", event => {
const allTasks = document.querySelectorAll("li");
allTasks.forEach(item =>{
item.remove();
})
updateMessage();
});
function filterTask(term){
Array.from(tasks.children)
.filter(task =>{
return !task.textContent.toLowerCase().includes(term);
})
.forEach(task =>{
task.classList.add("hide");
});
Array.from(tasks.children)
.filter(task =>{
return task.textContent.toLowerCase().includes(term);
})
.forEach(task =>{
task.classList.remove("hide");
});
}
search.addEventListener("keyup", event =>{
const term = search.task.value.trim().toLowerCase();
filterTask(term);
});
search.addEventListener("click", event =>{
if(event.target.classList.contains("reset")){
search.reset();
}
const term = search.task.value.trim().toLowerCase();
filterTask(term);
});