-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (62 loc) · 1.93 KB
/
index.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
const inputBox = document.querySelector(".inputF input");
const addbtn = document.querySelector(".inputF button");
const todolist = document.querySelector(".todoList");
const deleteAllbtn = document.querySelector(".footer button")
inputBox.onkeyup = ()=>{
let userData = inputBox.value;
if(userData.trim() !=0){
addbtn.classList.add("active");
}else{
addbtn.classList.remove("active");
}
}
addbtn.onclick = ()=>{
let userData = inputBox.value;
let getLocalStorage = localStorage.getItem("New Todo");
if(getLocalStorage == null){
listArr = [];
}
else{
listArr = JSON.parse(getLocalStorage);
}
listArr.push(userData);
localStorage.setItem("New Todo", JSON.stringify(listArr));
showTasks();
addbtn.classList.remove("active");
}
function showTasks()
{
let getLocalStorage = localStorage.getItem("New Todo");
if(getLocalStorage == null){
listArr = [];
}
else{
listArr = JSON.parse(getLocalStorage);
}
const pendingNumb = document.querySelector(".pendingNumb");
pendingNumb.textContent = listArr.length;
if(listArr.length > 0){
deleteAllbtn.classList.add("active");
}
else{
deleteAllbtn.classList.remove("active");
}
let newLiTag = '';
listArr.forEach((element, index) =>{
newLiTag += `<li> ${element} <span onclick = "deleteTask(${index})"; ><i class="bx bxs-trash"></i></span></li>`;
});
todolist.innerHTML = newLiTag;
inputBox.value = "";
}
function deleteTask(index) {
let getLocalStorage = localStorage.getItem("New Todo");
listArr = JSON.parse(getLocalStorage);
listArr.splice(index, 1);
localStorage.setItem("New Todo", JSON.stringify(listArr));
showTasks();
}
deleteAllbtn.onclick = ()=>{
listArr = [];
localStorage.setItem("New Todo", JSON.stringify(listArr));
showTasks();
}