-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
69 lines (65 loc) · 2.2 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
66
67
68
69
// get all important elements
const inputBox = document.querySelector(".inputField input");
const addBtn = document.querySelector(".inputField button");
const todoList = document.querySelector(".todoList");
const deleteAllBtn = document.querySelector(".footer button");
//all needed functions
//when the user fills the input
inputBox.onkeyup = ()=>{
let valueEntered = inputBox.value;
if (valueEntered.trim != 0 ) addBtn.classList.add("active");
else addBtn.classList.remove("active");
};
//show all tasks
let showTasks = () => {
let lclStorage = localStorage.getItem("Todo");
if(lclStorage == null){
listArray = [];
}else{
listArray = JSON.parse(lclStorage);
}
const tasksNum = document.querySelector(".number");
tasksNum.textContent = listArray.length;
if (listArray.length == 0) {
deleteAllBtn.classList.remove("active");
} else {
deleteAllBtn.classList.add("active");
}
let liTag = "";
listArray.forEach((element,index) => {
liTag += `<li>${element}<span class="icon" onclick="deleteTask(${index})"><i class="bi bi-x-circle-fill"></i></span></li>`;
}
);
todoList.innerHTML = liTag;
inputBox.value= "";
};
//when the user adds a tesk //press add button
addBtn.onclick = ()=>{
let valueEntered = inputBox.value;
let lclStorage = localStorage.getItem("Todo");
if (lclStorage == null ) {
listArray = []; // create an empty array
} else {
listArray = JSON.parse(lclStorage);
}
listArray.push(valueEntered); // add the new todo to the array
localStorage.setItem("Todo" , JSON.stringify(listArray));
//call the showTasks function
showTasks();
addBtn.classList.remove("active");
};
// delete task function
function deleteTask(index){
let lclStorageData = localStorage.getItem("Todo");
listArray = JSON.parse(lclStorageData);
listArray.splice(index, 1); //delete or remove the li
localStorage.setItem("Todo", JSON.stringify(listArray));
showTasks(); //call the showTasks function
}
// delete all tasks function
deleteAllBtn.onclick = ()=>{
listArray = []; //empty the array
localStorage.setItem("Todo", JSON.stringify(listArray)); //set the item in localstorage
showTasks(); //call the showTasks function
};
showTasks();