-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.html
128 lines (111 loc) · 3.27 KB
/
list.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Todo List</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
.container {
margin: 20px;
}
h1 {
text-align: center;
}
form {
display: flex;
margin-bottom: 20px;
}
input[type="text"] {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px 0 0 5px;
}
button[type="submit"] {
padding: 10px;
background-color: #2ecc71;
color: #fff;
border: none;
border-radius: 0 5px 5px 0;
cursor: pointer;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border-bottom: 1px solid #ccc;
}
li .todo-item {
flex: 1;
margin-right: 10px;
}
li button {
padding: 5px;
background-color: #e74c3c;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>Todo List</h1>
<form id="todo-form">
<input type="text" placeholder="Add todo">
<button type="submit">Add</button>
</form>
<ul id="todo-list"></ul>
</div>
<script>
const form = document.getElementById('todo-form');
const input = form.querySelector('input');
const list = document.getElementById('todo-list');
let todos = [];
function renderTodoList() {
list.innerHTML = '';
todos.forEach((todo, index) => {
const li = document.createElement('li');
const todoItem = document.createElement('span');
todoItem.classList.add('todo-item');
todoItem.innerText = todo;
const deleteButton = document.createElement('button');
deleteButton.innerText = 'Delete';
deleteButton.addEventListener('click', () => {
todos.splice(index, 1);
localStorage.setItem('todos', JSON.stringify(todos));
renderTodoList();
});
li.appendChild(todoItem);
li.appendChild(deleteButton);
list.appendChild(li);
});
}
form.addEventListener('submit', event => {
event.preventDefault();
const todo = input.value.trim();
if (todo) {
todos.push(todo);
localStorage.setItem('todos', JSON.stringify(todos));
input.value = '';
renderTodoList();
}
});
if (localStorage.getItem('todos')) {
todos = JSON.parse(localStorage.getItem('todos'));
}
renderTodoList();
</script>
</body>
</html>