-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (34 loc) · 1.16 KB
/
index.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
const input = document.getElementById("add-box");
const addBtn = document.getElementById("add-todo");
const list = document.getElementById("list")
function addTodo() {
if (input.value === "") {
alert("Please enter in some text to the input box :)")
} else {
let listItem = document.createElement("li");
listItem.innerHTML = input.value.trim();
let cancel = document.createElement("span");
cancel.innerHTML = "\u00d7";
listItem.appendChild(cancel);
list.appendChild(listItem);
cancel.addEventListener("click", removeTodo);
listItem.addEventListener("click", toggleChecked);
}
input.value = "";
}
function removeTodo(event) {
event.target.parentElement.remove();
}
function toggleChecked(event) {
event.target.classList.toggle("checked")
}
addBtn.addEventListener("click", addTodo);
function handleKeyPress(event) {
if (input.value.trim() === "" && event.keyCode === 13) {
alert("Please enter in some text to the input box :)")
}
if (input.value.trim() !== "" && event.keyCode === 13) {
addTodo();
}
}
input.addEventListener("keypress", handleKeyPress)