-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (60 loc) · 2.16 KB
/
app.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
71
72
73
74
75
76
const addForm = document.querySelector('.add');
const list = document.querySelector('.todos');
const search = document.querySelector('.search input');
// functions
const generateTemplate = todo => {
const newTask = `
<li class="list-group-item my-1 d-flex justify-content-between align-items-center">
<span>${todo}</span>
<div>
<i class="fa-solid fa-check mx-2 check" style="color: #61d006;"></i>
<i class="fa-solid fa-xmark delete" style="color: #df3826;"></i>
</div>
</li>
`;
list.innerHTML += newTask;
};
const filterTasks = term => {
Array.from(list.children)
.filter((task) => {
return !task.textContent.toLowerCase().includes(term);
})
.forEach((task) => {
task.classList.add('filtered');
})
Array.from(list.children)
.filter((task) => {
return task.textContent.toLowerCase().includes(term);
})
.forEach((task) => {
task.classList.remove('filtered');
})
// Array.from(list.children)
// .filter(task => !task.textContent.toLocaleLowerCase().includes(term))
// .forEach(task => task.classList.add('filtered'));
// Array.from(list.children)
// .filter(task => task.textContent.toLocaleLowerCase().includes(term))
// .forEach(task => task.classList.remove('filtered'));
};
//add tasks
addForm.addEventListener('submit', e => {
e.preventDefault();
const todo = addForm.add.value.trim(); //get the keyboard input and trim by space
if(todo.length){ //check for an actual value
generateTemplate(todo);
addForm.reset(); //clear the form
}
});
//edit tasks
list.addEventListener('click', e => {
if(e.target.classList.contains('delete')){
e.target.parentElement.parentElement.remove();
} else if(e.target.classList.contains('check')){
e.target.parentElement.previousElementSibling.classList.add('text-decoration-line-through');
}
});
//search
search.addEventListener('keyup', () => {
const term = search.value.trim().toLowerCase();
filterTasks(term);
});