-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (71 loc) · 2.3 KB
/
script.js
File metadata and controls
85 lines (71 loc) · 2.3 KB
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
77
78
79
80
81
82
83
84
85
var enterBtn = document.getElementById("enter");
var input = document.getElementById("userinput");
var ul = document.querySelector("ul");
var btn = document.createElement("button");
/* Creates a new list item from input, adds it to the list and
clears the text field */
var createListElement = () => {
// Creates a li element and append it to ul
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
input.value="";
// Creates the delete button and append it to the li item.
var deleteBtn = document.createElement("button");
deleteBtn.appendChild(document.createTextNode("X"));
li.appendChild(deleteBtn);
deleteBtn.addEventListener("click", () => {
li.parentNode.removeChild(li);
});
// Works (alt 2)
//deleteBtn.addEventListener("click", removeListItem(li));
//Works (alt 3)
/* deleteBtn.onclick = function() {
li.parentNode.removeChild(li);
} */
};
/*
// removeListItem with inner function "so you need something where li
// will live until the button is clicked, and that thing is called Closures "
var removeListItem = (listItem) => {
return () => {
listItem.parentNode.removeChild(listItem);
console.log("check");
};
};
*/
// Returns the input length.
var inputLength = () => {
return input.value.length;
};
// List item is added when button is pressed.
var addListAfterClick = () => {
if (inputLength() > 0) {
createListElement();
}
};
// List item is added when "Enter" key is pressed.
var addListAfterKeypress = (event) => {
if (inputLength() > 0 && event.code === "Enter") {
createListElement();
}
};
/* Adds an eventlistener to ul and checkes after "click". It also checks
to see if the parent element of the clicked element is the one that the
click event is attached to. Works for children of li too. */
ul.addEventListener('click', function (event) {
var target = event.target; // Clicked element
while (target && target.parentNode !== ul) {
/* If the clicked element isn't a direct child,
it sets the target as the parent of the current node */
target = target.parentNode;
if(!target) {
return; // If element doesn't exist
}
}
if (target.tagName === 'LI'){
target.classList.toggle("done");
}
});
enterBtn.addEventListener("click", addListAfterClick);
input.addEventListener("keydown", addListAfterKeypress);