-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (58 loc) · 1.68 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 1. If you click on the list item, it toggles the .done
//class on and off.
// 2. Add buttons next to each list item to delete
//the item when clicked on its corresponding delete button.
// 3. BONUS: When adding a new list item, it automatically
//adds the delete button next to it (hint: be sure to check
//if new items are clickable too!)
var button = document.getElementById("enter");
var input = document.getElementById("userinput");
var ul = document.querySelector("ul");
var numList = document.querySelectorAll("li");
function listLength() {
return numList.length;
}
function inputLength() {
return input.value.length;
}
function createListElement() {
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
input.value = "";
var del = document.createElement("button");
del.innerHTML = "DELETE";
del.classList.add("delete");
li.appendChild(del);
ul.appendChild(li);
for (var i=0; i < listLength() ;i++) {
del.classList.add("i");
li.classList.add("i");
}
}
function addListAfterClick() {
if (inputLength() > 0) {
createListElement();
}
}
function addListAfterKeypress(event) {
if (inputLength() > 0 && event.keyCode === 13) {
createListElement();
}
}
function crossOut(event) {
var target = event.target;
if (target.matches("li")) {
target.classList.toggle("done");
}
}
function deleteClick(event) {
var target2 = event.target
if (target2.matches(".delete")) {
target2.parentNode.remove();
}
}
button.addEventListener("click", addListAfterClick);
input.addEventListener("keypress", addListAfterKeypress);
ul.addEventListener("click", crossOut);
ul.addEventListener("click", deleteClick);