-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.html
104 lines (78 loc) · 2.06 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
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.done {
text-decoration: line-through;
}
.task-name {
display: inline-block;
width: 100px;
}
</style>
</head>
<body>
<input type="text" id="new-task" placeholder="Add your task...">
<button id="add-task">Add</button>
<ul id="task-list"></ul>
<button id="clear-list">Clear All Tasks</button>
<script>
var tasks = [];
document.getElementById("add-task").addEventListener('click', addTask);
document.getElementById("clear-list").addEventListener('click', function() {
tasks = [];
listTasks();
});
function addTask() {
let newTask = document.getElementById("new-task");
if (newTask.value != '') {
tasks.push(newTask.value);
newTask.value = '';
listTasks();
}
}
function listTasks() {
let taskList = document.getElementById("task-list");
taskList.innerHTML = '';
tasks.forEach(function(task) {
taskItem = document.createElement('li');
taskDone = document.createElement('input');
taskDone.type = 'checkbox';
taskDone.addEventListener('change', function(event) {
if (event.target.checked) {
event.target.nextSibling.classList.add('done');
}
else {
event.target.nextSibling.classList.remove('done');
}
});
taskName = document.createElement('span');
taskName.classList.add('task-name');
taskName.innerText = task;
taskDelete = document.createElement('button');
taskDelete.innerHTML = '×';
taskDelete.addEventListener('click', function(event) {
removeTask(event.target.previousSibling.innerText);
});
taskItem.appendChild(taskDone);
taskItem.appendChild(taskName);
taskItem.appendChild(taskDelete);
taskList.appendChild(taskItem);
});
}
function removeTask(task) {
tasks = tasks.filter(function(element) {
if (element == task) {
return false;
}
else {
return true;
}
});
listTasks();
}
</script>
</body>
</html>