-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (70 loc) · 2.89 KB
/
script.js
File metadata and controls
76 lines (70 loc) · 2.89 KB
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
const inputBox = document.querySelector(".inputField input");
const addButton = document.querySelector(".inputField button");
const todoList = document.querySelector(".todoList");
const clearAllButton = document.querySelector(".footer button");
inputBox.onkeyup = ()=>{
let userData = inputBox.value; //getting what user enter in the input box.
if(userData.trim() !=0) // if user values aren't only spaces.
{
addButton.classList.add("active") //active add button..;
}
else {
addButton.classList.remove("active") //unactive add button..;
}
}
showT(); // calling function
//what if user click on add buttxon..
addButton.onclick = ()=>{
let userData = inputBox.value; //getting what user enter in the input box.
let getLocalStorage = localStorage.getItem("New to-do");// getting local storage;
if(getLocalStorage == null){
listAr = [];// create a empty array;
}
else{
listAr = JSON.parse(getLocalStorage);// transform json string to js objects
}
listAr.push(userData);// push or add user data
localStorage.setItem("New to-do",JSON.stringify(listAr));// transform js objects to json string
showT(); // calling function
addButton.classList.remove("active") //unactive add button..;
}
//function to add task list inside ul
function showT(){
let getLocalStorage = localStorage.getItem("New to-do");// getting local storage;
if(getLocalStorage == null){
listAr = [];// create a empty array;
}
else{
listAr = JSON.parse(getLocalStorage);// transform json string to js objects
}
const pendingnum = document.querySelector(".pendingnum");
pendingnum.textContent = listAr.length; //size of remaining task;
if(listAr.length > 0){
clearAllButton.classList.add("active");
}
else {
clearAllButton.classList.remove("active"); //if array size is 0 then inactive the clear all button;
}
let newTag = '';
listAr.forEach((element, index) => {
newTag += `<li>${element}<span onclick = "deleteT(${index})"; ><i class="fas fa-trash"></i></span></li>`;
});
todoList.innerHTML = newTag; // adding a new list tag inside ul tag
inputBox.value = "";
}
//delete function
function deleteT(index){
let getLocalStorage = localStorage.getItem("New to-do");// getting local storage;
listAr = JSON.parse(getLocalStorage);// transform json string to js objects
listAr.splice(index, 1); //delete the indexed li
//after remove update the local storage
localStorage.setItem("New to-do",JSON.stringify(listAr));// transform js objects to json string
showT(); // calling function
}
//for clear all the task from list
clearAllButton.onclick = () =>{
listAr = [] // o/p will be empty;
//after clear all lists
localStorage.setItem("New to-do",JSON.stringify(listAr));// transform js objects to json string
showT(); // calling function;
}