-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
62 lines (58 loc) · 1.89 KB
/
main.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
// All What I Need Of Variables
const inputTask = document.querySelector(".int-task");
const btnTask = document.querySelector(".btn-task");
const todoList = document.querySelector(".todoList");
const strong = document.querySelector("strong");
const btnClearAll = document.querySelector(".clear-btn");
// Show tasks
function showTasks() {
let getLocalStorage = localStorage.getItem("New todo");
if (getLocalStorage == null) {
listArr = [];
} else {
listArr = JSON.parse(getLocalStorage);
}
let newLineTag = ``;
for (const [i, n] of listArr.entries()) {
newLineTag += `<li>${n}<span onclick="removeFromStorage(${i})"><i class="fas fa-trash"></i></span></li>`;
}
todoList.innerHTML = newLineTag;
inputTask.value = "";
strong.textContent = listArr.length;
}
// live key up
inputTask.onkeyup = () => {
let userData = inputTask.value;
if (userData.trim() != 0) {
btnTask.classList.add("active");
} else {
btnTask.classList.remove("active");
}
};
showTasks();
// onclick in the button
//showTasks();
btnTask.addEventListener("click", function() {
let userData = inputTask.value;
let getLocalStorage = localStorage.getItem("New todo");
if (inputTask.value.trim() != "" && getLocalStorage == null) {
listArr = [];
} else if (inputTask.value.trim() != "") {
listArr = JSON.parse(getLocalStorage);
}
listArr.push(userData);
window.localStorage.setItem("New todo", JSON.stringify(listArr));
showTasks();
});
function removeFromStorage(index) {
let getLocalStorage = localStorage.getItem("New todo");
listArr = JSON.parse(getLocalStorage);
listArr.splice(index, 1);
//localStorage.clear();
window.localStorage.setItem("New todo", JSON.stringify(listArr));
showTasks();
}
btnClearAll.addEventListener("dblclick", function() {
window.localStorage.clear();
showTasks();
});