-
Notifications
You must be signed in to change notification settings - Fork 1
/
todo.html
155 lines (135 loc) · 3.87 KB
/
todo.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
155
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Simple TODO List</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}
html {
--primary: hsl(200, 100%, 5%);
--text: hsl(200, 100%, 50%);
font-size: 1.2rem;
}
body {
background-color: var(--primary);
color: var(--text);
text-align: center;
padding: 1rem;
}
section h1 {
font-size: 2rem;
}
div {
margin: 3rem auto;
}
form {
display: flex;
flex-flow: row;
margin: 2rem auto;
}
form input[type="text"] {
background-color: rgba(0, 0, 0, 0);
padding: 1rem;
font-size: 2rem;
border: 3px solid var(--text);
width: 100%;
border-right: none;
color: var(--text);
border-radius: 10px;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
::placeholder {
color: hsl(200, 100%, 10%);
}
form input[type="submit"] {
font-size: 1rem;
background-color: var(--text);
color: var(--primary);
width: 40%;
font-size: 2rem;
border: none;
font-weight: 600;
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
}
form input[type="submit"]:focus {
background-color: turquoise;
}
ul li {
text-align-last: left;
list-style-type: square;
font-weight: 600;
font-size: 2rem;
overflow-wrap: break-word;
margin-left: 2rem;
}
@media (min-width: 576px) {
section {
max-width: 576px;
margin: 0 auto;
}
}
</style>
</head>
<body>
<section>
<h1>Simple TODO List</h1>
<small>With <b>LocalStorage</b> Integration</small>
<div>
<form>
<!-- TODO Input Field -->
<input type="text" placeholder="Start brainstorming" autofocus />
<!-- Submit Button -->
<input type="submit" value="ADD" />
</form>
<!-- Todo List -->
<ul>
<!-- List Items -->
</ul>
</div>
</section>
<script>
const inputForm = document.querySelector("form");
const inputField = document.querySelector("input[type='text']");
const submitBtn = document.querySelector("input[type='submit']");
const ul = document.querySelector("ul");
const todoItems = [];
const arr = JSON.parse(localStorage.getItem("todos"));
// Load LocalStorage Items on Page Load
window.addEventListener("load", () => {
if (localStorage.length > 0) {
for (let i = 0; i < arr.length; i++) {
const li = document.createElement("li");
li.innerText = arr[i];
ul.appendChild(li);
}
}
});
// Prevent Default Events on Submit
inputForm.addEventListener("submit", function (e) {
e.preventDefault();
// Simple form validation
if (inputField.value !== "" && inputField.value.length >= 1) {
// Create a list item
const li = document.createElement("li");
li.innerText = inputField.value;
todoItems.push(inputField.value);
// Add the inputvalue with value to the localstorage
localStorage.setItem("todos", JSON.stringify(todoItems));
// Append the li to the ul
ul.appendChild(li);
inputField.value = "";
}
});
</script>
</body>
</html>