-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
76 lines (53 loc) · 1.6 KB
/
todo.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
const addTask = document.querySelector('.add');
const taskItems = document.querySelector('.items')
const searchItems = document.querySelector('.search input');
//create a function that will add the tasks
const todolist = (theTask) => {
//pass in the theTask
const html =
`<li class="item">
<h4>${theTask}</h4>
<i class="fas fa-trash"></i>
</li>`;
//injecting theTask to the ul
taskItems.innerHTML += html;
}
/* add a task*/
addTask.addEventListener('submit', e => {
e.preventDefault();
const theTask = addTask.task.value.trim();
//prevent submitting an empty task
if(theTask.length){
todolist(theTask);
addTask.reset();
}
});
/*delete tasks*/
taskItems.addEventListener('click', e => {
if(e.target.classList.contains('fa-trash')){
e.target.parentElement.remove();
}
});
const filteredWords = (searchWords)=> {
//change tthe htmlcollection into an array
Array.from(taskItems.children)
.filter((todotasks) => {
return !todotasks.textContent.toLowerCase().includes(searchWords);
})
.forEach((todotasks) => {
todotasks.classList.add('filtered');
})
Array.from(taskItems.children)
.filter((todotasks) => {
return todotasks.textContent.toLowerCase().includes(searchWords);
})
.forEach((todotasks) => {
todotasks.classList.remove('filtered');
})
};
/*filter tasks*/
searchItems.addEventListener('keyup', () => {
//get the search
const searchWords = searchItems.value.trim().toLowerCase();
filteredWords(searchWords);
});