-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (62 loc) · 1.97 KB
/
script.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
const inputBox = document.querySelector(".inputField input");
const addBtn = document.querySelector(".inputField button");
const todoList = document.querySelector(".todoList");
const deleteAllBtn = document.querySelector(".footer button");
// onkeyup event
inputBox.onkeyup = ()=>{
let userEnteredValue = inputBox.value;
if(userEnteredValue.trim() != 0){
addBtn.classList.add("active");
}else{
addBtn.classList.remove("active");
}
}
showTasks();
addBtn.onclick = ()=>{
let userEnteredValue = inputBox.value;
let getLocalStorageData = localStorage.getItem("New Todo");
if(getLocalStorageData == null){
listArray = [];
}else{
listArray = JSON.parse(getLocalStorageData);
}
listArray.push(userEnteredValue);
localStorage.setItem("New Todo", JSON.stringify(listArray));
showTasks();
addBtn.classList.remove("active");
}
function showTasks(){
let getLocalStorageData = localStorage.getItem("New Todo");
if(getLocalStorageData == null){
listArray = [];
}else{
listArray = JSON.parse(getLocalStorageData);
}
const pendingTasksNumb = document.querySelector(".pendingTasks");
pendingTasksNumb.textContent = listArray.length;
if(listArray.length > 0){
deleteAllBtn.classList.add("active");
}else{
deleteAllBtn.classList.remove("active");
}
let newLiTag = "";
listArray.forEach((element, index) => {
newLiTag += `<li>${element}<span class="icon" onclick="deleteTask(${index})"><i class="fas fa-trash"></i></span></li>`;
});
todoList.innerHTML = newLiTag;
inputBox.value = "";
}
// delete task function
function deleteTask(index){
let getLocalStorageData = localStorage.getItem("New Todo");
listArray = JSON.parse(getLocalStorageData);
listArray.splice(index, 1);
localStorage.setItem("New Todo", JSON.stringify(listArray));
showTasks();
}
// delete all tasks function
deleteAllBtn.onclick = ()=>{
listArray = [];
localStorage.setItem("New Todo", JSON.stringify(listArray));
showTasks();
}