-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappday18.js
51 lines (43 loc) · 1.46 KB
/
appday18.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
const form = document.querySelector('#task-form');
const taskList = document.querySelector('.collection');
const clearBtn = document.querySelector('.clear-task');
const filter = document.querySelector('#filter');
const taskInput = document.querySelector('#task');
loadEventListeners();
function loadEventListeners() {
form.addEventListener('submit', addTask);
taskList.addEventListener('click', removeTask);
filter.addEventListener('keyup', filterTask);
}
function addTask(e) {
const li = document.createElement('li');
li.className = "collection-item";
li.appendChild(document.createTextNode(taskInput.value));
const link = document.createElement('a');
link.className = "delete-item";
link.innerHTML = '<i class="material-icons">delete</i>'
li.appendChild(link);
taskList.appendChild(li);
e.preventDefault();
}
function removeTask(e) {
if (e.target.parentElement.classList.contains('delete-item'))
e.target.parentElement.parentElement.remove();
}
function clearTask() {
taskList.innerHTML = '';
}
function filterTask(e) {
const text = e.target.value.toLowerCase();
document.querySelectorAll('.collection-item').forEach(
function (task) {
const item = task.firstChild.textContent;
if (item.toLowerCase().indexOf(text) != -1) {
task.style.display = 'block';
}
else {
task.style.display = 'none';
}
}
);
}