-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (55 loc) · 2.27 KB
/
script.js
File metadata and controls
81 lines (55 loc) · 2.27 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
const addTaskInput = document.querySelector("#addTask");
const addBtn = document.querySelector("#addBtn");
const removeTaskDiv = document.querySelector("#removeTaskDiv");
const list = document.querySelector("ul");
const infoBox = document.querySelector("strong");
const nrOfcompleted = document.querySelector("#nrOfCompleted p");
const taskArray = [];
let counter = 0;
nrOfcompleted.textContent = `Completed tasks: ${counter}`;
addBtn.addEventListener("click", function () {
if (addTaskInput.value.length > 0) {
infoBox.innerText = "";
}
else {
infoBox.innerText = "You have to type something";
return;
}
taskArray.push(addTaskInput.value);
//create an li and add to the ul.
const liElement = document.createElement("li");
liElement.innerText = addTask.value;
list.appendChild(liElement);
//create a remove-button with an img.
const removeTaskBtn = document.createElement("button");
const removeTaskImg = document.createElement("img");
removeTaskImg.src = "trashcan2.png";
removeTaskDiv.appendChild(removeTaskBtn);
removeTaskBtn.appendChild(removeTaskImg);
addTask.value = "";
liElement.addEventListener("click", function () {
if (liElement.getAttribute("class") == "completed") {
liElement.setAttribute("class", "");
counter--;
}
else {
liElement.setAttribute("class", "completed");
counter++;
}
nrOfcompleted.textContent = `Completed tasks: ${counter}`;
});
removeTaskBtn.addEventListener("click", function () {
const taskArrayElement = taskArray.indexOf(liElement.innerText);
//if it returns >=0, it means that the element exists in the array and matches the liElement-text. -1 means that it does not exist.
if (taskArrayElement >= 0) {
// 1 means delete only one element. 2 means delete that element and the element after.
taskArray.splice(taskArrayElement, 1);
}
liElement.remove();
removeTaskBtn.remove();
if(counter != 0 && liElement.getAttribute("class") == "completed"){
counter--;
nrOfcompleted.textContent = `Completed tasks: ${counter}`;
}
});
});