-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
47 lines (42 loc) · 1.25 KB
/
todo.js
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
var tasks = [];
var input = document.getElementById("input");
var taskList = document.getElementById("taskList");
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("addButton").click();
}
});
function addTask() {
var taskName = input.value;
if (taskName !== "") {
var task = {
name: taskName,
completed: false
};
tasks.push(task);
var li = document.createElement("li");
li.innerHTML = taskName;
li.addEventListener("click", function() {
li.classList.toggle("checked");
task.completed = !task.completed;
});
// Add a span element with a delete button to the list item
var span = document.createElement("span");
span.innerHTML = "×";
span.addEventListener("click", function() {
var clearButton = document.createElement("button");
clearButton.innerHTML = "Clear";
clearButton.addEventListener("click", function() {
taskList.removeChild(li);
tasks.splice(tasks.indexOf(task), 1);
});
li.appendChild(clearButton);
li.style.textDecoration = "line-through";
task.completed = !task.completed;
});
li.appendChild(span);
taskList.appendChild(li);
input.value = "";
}
}