-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
113 lines (89 loc) · 2.85 KB
/
javascript.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var userInput = document.getElementById("userInput");
var submitInput = document.getElementById("submitInput");
var taskList = document.querySelector("#taskList");
var deleteAllTasks = document.querySelector("#deleteAllTasks");
var colorPicker = document.querySelector("#colorPicker");
var deleteSelected = document.querySelector("#deleteSelected");
var checkBoxes = document.getElementsByClassName("checkBox");
var tasks = taskList.children;
function inputLenght () {
return userInput.value.length;
};
//Delete selected tasks - Forward loop
// deleteSelected.onclick = function () {
// let listLength = taskList.children.length;
// for (let i = 0; i <= listLength; i++) {
// if ( checkBoxes[i].checked === true ) {
// checkBoxes[i].parentElement.parentElement.remove();
// listLength = listLength + 1;
// }
// }
// };
//Delete selected tasks - Reverse loop
deleteSelected.onclick = function () {
for (let i = taskList.children.length - 1 ; i>0 ; i--) {
if (tasks[i].checked === true) {
tasks.Items.removeAt(i);
}
}
};
function addTask () {
//Criar novo div
var thirdDiv = document.querySelector("#thirdDiv");
var newDiv1 = document.createElement("div");
var newDiv2 = document.createElement("div");
newDiv2.className = "newDiv2";
newDiv1.className = "newDiv1";
//Adicionar tarefa à lista de tarefas
var newTask = document.createElement("li");
newTask.className = "taskPending";
newTask.appendChild(document.createTextNode(userInput.value));
//Marcar tarefa como concluída
newTask.onclick = function() {
if (newTask.className === "taskPending") {
newTask.className = "taskDone";
}
else {
newTask.className = "taskPending";
};
};
//Criar check-box para cada tarefa
var checkBox = document.createElement("input");
checkBox.type = "checkBox";
checkBox.className = "checkBox";
// Append children
newDiv2.appendChild(checkBox);
newTask.appendChild(newDiv2);
newDiv1.appendChild(newTask);
taskList.appendChild(newDiv1);
//Criar botão delete para cada tarefa
var deleteButton = document.createElement("button");
deleteButton.appendChild(document.createTextNode("Delete"));
newDiv2.appendChild(deleteButton);
//Apagar texto após criar nova tarefa
userInput.value = "";
//Botão para deletar tarefa especifica
deleteButton.onclick = function deleteTask () {
newTask.remove();
};
newDiv1.style.backgroundColor = colorPicker.value ;
};
submitInput.addEventListener("click" , function () {
if (inputLenght () > 0) {
addTask ();
}
else {
alert("The task must be described before submiting");
}
});
userInput.addEventListener("keypress" , function (event) {
if ( inputLenght () > 0 && event.which === 13 ) {
addTask ();
}
else if ( event.which === 13 ) {
alert("The task must be described before submiting");
}
});
deleteAllTasks.addEventListener("click" , function deleteTaskList() {
taskList.innerHTML = "";
});