-
Notifications
You must be signed in to change notification settings - Fork 0
/
todolist.html
154 lines (115 loc) · 4.35 KB
/
todolist.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ToDo List</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/all.min.css">
</head>
<body>
<nav class="navbar navbar-dark bg-info">
<div class="container" style="max-width: 600px;">
<a href="#" class="navbar-brand">
Todo
<span class="badge bg-danger ms-2">0</span>
</a>
<ul class="navbar-nav">
<li class="nav-item">
<a href="#" id="clear" class="nav-link">Clear</a>
</li>
</ul>
</div>
</nav>
<div class="container" style="max-width: 600px;">
<div class="input-group my-4">
<input type="text" class="form-control">
<button class="btn btn-primary">Add</button>
</div>
<ul id="todo" class="list-group"></ul>
<ul id="done" class="list-group mt-4 opacity-50"></ul>
</div>
<script>
function removeData(text) {
let data = getData();
let result = data.filter(item => item.text != text);
localStorage.setItem('data', JSON.stringify(result));
}
function checkData(text) {
let data = getData();
let result = data.map(item => {
if(item.text == text) item.done = true;
return item;
});
localStorage.setItem('data', JSON.stringify(result));
}
function clearData() {
let data = getData();
let result = data.filter(item => !item.done);
localStorage.setItem('data',JSON.stringify(result));
}
// item store on localStorage
getData().map(item => createTask(item.text, item.done));
function getData() {
return JSON.parse(localStorage.getItem("data")) || [];
}
function saveData(text) {
let data = getData();
data.push = getData();
data.push({ text, done: false});
localStorage.setItem('data',JSON.stringify(data));
}
function updateCount() {
document.querySelector(".badge").textContent =
document.querySelectorAll("#todo li").length;
}
document.querySelector("#clear").onclick = () => {
document.querySelector("#done").textContent = "";
}
document.querySelector("button").onclick = () => {
let text = document.querySelector("input").value;
createTask(text);
updateCount();
saveData(text);
document.querySelector("input").value ="";
document.querySelector("input").focus();
}
document.querySelector("input").onkeydown = e => {
if(e.key == "Enter") {
document.querySelector("button").onclick();
}
}
function createTask(text, done = false) {
let li = document.createElement("li");
li.classList.add("list-group-item");
li.textContent = text;
let del = document.createElement("a");
del.setAttribute("href", "#");
del.classList.add("fa-solid", "fa-trash","text-danger","float-end");
del.onclick=()=>{
li.remove();
updatecount();
removeData(text);
}
li.appendChild(del);
let check = document.createElement("a");
check.setAttribute("href", "#");
check.classList.add("fa-solid", "fa-check", "float-start", "me-3");
check.onclick = () => {
document.querySelector("#done").appendChild(li);
check.remove();
updatecount();
checkData(text);
}
if(!done) {
li.appendChild(check);
}
if(done) {
document.querySelector("#done").appendChild(li);
} else {
document.querySelector("#todo").appendChild(li);
}
}
</script>
</body>
</html>