-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (45 loc) · 1.26 KB
/
script.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
50
51
52
53
54
55
56
var button = document.getElementById("enter");
var input = document.getElementById("userinput");
var ul = document.querySelector("ul");
function inputLength() {
return input.value.length;
}
function createListElement() {
var div = document.createElement("div");
var li = document.createElement("li");
var delButton = document.createElement("button");
div.classList.add("wrapper");
ul.appendChild(div);
div.append(li, delButton);
li.classList.add("taskClass");
li.appendChild(document.createTextNode(input.value));
input.value = "";
delButton.classList.add("delClass");
}
function addListAfterClick() {
if (inputLength() > 0) {
createListElement();
}
}
function addListAfterKeypress(event) {
if (inputLength() > 0 && event.keyCode === 13) {
createListElement();
}
}
function doneTask(task) {
if (task.target.tagName === "LI") {
task.target.classList.toggle("done");
}
}
function deleteListElement(element) {
if (element.target.className === "delClass") {
element.target.parentElement.remove();
}
}
function handleUlClick(element) {
doneTask(element);
deleteListElement(element);
}
ul.addEventListener("click", handleUlClick);
button.addEventListener("click", addListAfterClick);
input.addEventListener("keypress", addListAfterKeypress);