-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (40 loc) · 1.12 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
45
46
47
48
49
const enterButton = document.getElementById("enter");
const input = document.getElementById("input");
const list = document.querySelector("ul");
const tasks = document.querySelector("li");
// console.log (enterButton, input, list, item);
function lengthOfInput() {
return input.value.length;
}
function listElement() {
const li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
list.appendChild(li);
function crossOutTask() {
li.classList.toggle("done");
}
li.addEventListener("click", crossOutTask);
const deleteBtn = document.createElement("button");
deleteBtn.appendChild(document.createTextNode("X"));
li.appendChild(deleteBtn);
deleteBtn.addEventListener("click", deleteListItem);
function deleteListItem() {
li.classList.add("delete");
setTimeout(function () {
li.style.display = "none";
}, 500);
}
}
// listElement();
function addTask() {
if (lengthOfInput() > 0) {
listElement();
input.value = "";
}
}
enterButton.onclick = () => addTask();
input.addEventListener("keypress", () => {
if (event.which === 13) {
addTask();
}
});