-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (115 loc) · 3.64 KB
/
Copy pathscript.js
File metadata and controls
133 lines (115 loc) · 3.64 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const inputBox=document.getElementById("inputBox");
const addBtn=document.getElementById("addBtn");
const todolist=document.getElementById("todolist");
let editTodo=null;
//Function to add todo
const addTodo=()=>{
const inputText=inputBox.value.trim();
if(inputText.length<=0){
alert("You must write something in your to do");
return false;
}
if(addBtn.value==="Edit"){
editTodo.target.previousElementSibling.innerHTML=inputText;
editLocalTodos(inputText);
addBtn.value="Add";
inputBox.value="";
}
else{
//Creating p tag
const li=document.createElement("li");
const p=document.createElement("p");
p.innerHTML=inputText;
li.appendChild(p);
//Creating edit button
const editBtn=document.createElement("button");
editBtn.innerText="Edit";
editBtn.classList.add("btn","editBtn");
li.appendChild(editBtn);
//Creating delete button
const deleteBtn=document.createElement("button");
deleteBtn.innerText="Remove";
deleteBtn.classList.add("btn","deleteBtn");
li.appendChild(deleteBtn);
todolist.appendChild(li);
inputBox.value="";
saveLocalTodos(inputText);
}
}
//Function to edit todo
const updateTodo=(e)=>{
if(e.target.innerHTML==="Remove"){
todolist.removeChild(e.target.parentElement);
deleteLocalTodos(e.target.parentElement);
}
if(e.target.innerHTML==="Edit"){
inputBox.value=e.target.previousElementSibling.innerHTML;
inputBox.focus();
addBtn.value="Edit";
editTodo=e;
}
}
//Function to save local todo
const saveLocalTodos=(todo)=>{
let todos=[];
if(localStorage.getItem("todos")===null){
todos=[];
}
else{
todos=JSON.parse(localStorage.getItem("todos"));
}
todos.push(todo);
localStorage.setItem("todos",JSON.stringify(todos));
}
//Function to get local todo
const getLocalTodos=()=>{
let todos=[];
if(localStorage.getItem("todos")===null){
todos=[];
}
else{
todos=JSON.parse(localStorage.getItem("todos"));
todos.forEach(todo => {
//Creating p tag
const li=document.createElement("li");
const p=document.createElement("p");
p.innerHTML=todo;
li.appendChild(p);
//Creating edit button
const editBtn=document.createElement("button");
editBtn.innerText="Edit";
editBtn.classList.add("btn","editBtn");
li.appendChild(editBtn);
//Creating delete button
const deleteBtn=document.createElement("button");
deleteBtn.innerText="Remove";
deleteBtn.classList.add("btn","deleteBtn");
li.appendChild(deleteBtn);
todolist.appendChild(li);
});
}
}
//Function to delete local todo
const deleteLocalTodos=(todo)=>{
let todos=[];
if(localStorage.getItem("todos")===null){
todos=[];
}
else{
todos=JSON.parse(localStorage.getItem("todos"));
}
let todoText= todo.children[0].innerHTML;
let todoIndex= todos.indexOf(todoText);
todos.splice(todoIndex,1);
localStorage.setItem("todos",JSON.stringify(todos));
console.log(todoIndex);
}
const editLocalTodos=(todo)=>{
let todos= JSON.parse(localStorage.getItem("todos"));
let todoIndex=todos.indexOf(todo);
todos[todoIndex]=inputBox.value;
localStorage.setItem("todos",JSON.stringify(todos));
}
document.addEventListener('DOMContentLoaded',getLocalTodos);
addBtn.addEventListener('click',addTodo);
todolist.addEventListener('click',updateTodo);