-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodo.html
102 lines (95 loc) · 2.69 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enhanced To-Do List</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 500px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
}
#todo-form {
display: flex;
margin-bottom: 20px;
}
#todo-input {
flex-grow: 1;
padding: 5px;
font-size: 16px;
}
button {
padding: 5px 10px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
background-color: #f0f0f0;
margin-bottom: 5px;
cursor: pointer;
}
li.done {
text-decoration: line-through;
opacity: 0.6;
}
.delete-btn {
background-color: #f44336;
}
.task-text {
flex-grow: 1;
margin-right: 10px;
}
</style>
</head>
<body>
<h1>To-Do List</h1>
<form id="todo-form">
<input type="text" id="todo-input" placeholder="Enter a new task" required>
<button type="submit">Add</button>
</form>
<ul id="todo-list"></ul>
<script>
const form = document.getElementById('todo-form');
const input = document.getElementById('todo-input');
const todoList = document.getElementById('todo-list');
form.addEventListener('submit', function(e) {
e.preventDefault();
addTodo();
});
function addTodo() {
if (input.value.trim() === '') return;
const li = document.createElement('li');
li.innerHTML = `
<span class="task-text">${input.value}</span>
<button class="delete-btn">Delete</button>
`;
li.addEventListener('click', function(e) {
if (e.target !== this && e.target.className !== 'task-text') return;
this.classList.toggle('done');
});
li.querySelector('.delete-btn').addEventListener('click', function(e) {
e.stopPropagation();
li.remove();
});
todoList.appendChild(li);
input.value = '';
}
</script>
</body>
</html>