-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
93 lines (75 loc) · 2.82 KB
/
app.js
File metadata and controls
93 lines (75 loc) · 2.82 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Define UI Variables
const form = document.querySelector("form");
const tasklist = document.querySelector(".collection");
const clearTaskBtn = document.querySelector(".clear-tasks");
const filter = document.querySelector("#filter");
const taskInput = document.querySelector("#task");
// ************************************************************************************
// Adding a task function
function addTask(e) {
if (taskInput.value === "") {
alert("Please add a task");
}
// Task A
// 1. If there is an input from the form, run this
const li = document.createElement("li");
// 2. Add a class to that newly created <li class="collection-item"></li>
li.className = "collection-item";
// 3. Create a text node from the taskInput
li.appendChild(document.createTextNode(taskInput.value));
// 4. Append that <li> to the tasklist <ul>
tasklist.appendChild(li);
// Task B
// Create a new link element for deleting task
const link = document.createElement("a");
// Give a class name
link.className = "delete-item secondary-content";
// Add an icon
link.innerHTML = '<i class="fa fa-remove"></i>';
// Append the link to the li
li.appendChild(link);
// Task C
// Clear input after submitting
taskInput.value = "";
// Prevents the default behaviour of a form
e.preventDefault();
}
// ********************************************************************************
// Remove Single task from the tasklist function
function removeTask(e) {
if (e.target.parentElement.classList.contains("delete-item")) {
e.target.parentElement.parentElement.remove();
}
}
// ******************************************************************************
// Clear all task function
function clearTasks(e) {
tasklist.innerHTML = "";
}
// ********************************************************************************
// Filter through tasks
function filterTasks(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";
}
});
}
// ********************************************************************************
// Load Event Listeners
function loadEventListeners() {
// Submit a task
form.addEventListener("submit", addTask);
//Remove a task
tasklist.addEventListener("click", removeTask);
// Clear all task via the Clear Task Button
clearTaskBtn.addEventListener("click", clearTasks);
// Filter through tasklist
filter.addEventListener("keyup", filterTasks);
}
loadEventListeners();
// *******************************************************************************