-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
87 lines (73 loc) · 2.16 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
77
78
79
80
81
82
83
84
85
86
87
var input = document.getElementById("userInput");
var button = document.getElementById("enter");
var ul = document.getElementsByTagName("ul")[0];
var list2 = document.querySelector("ul");
var btn = document.getElementsByClassName("bd");
var list = document.getElementsByTagName("li");
var j=0;
function addLiEvent() {
// Loops through all li's
for(i = 0; i < list.length; i++) {
// Listens for click event on delete button, then runs deleteListItem function
document.querySelectorAll(".bd")[i].addEventListener("click", deleteListItem);
}
}
//list input length
function inputLength() {
return input.value.length;
}
//click listener mouse
function addOnMouse() {
if (inputLength() > 0) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
var div = document.createElement("div");
ul.appendChild(div);
div.appendChild(li);
div.setAttribute("class", "listCont");
abcd();
addLiEvent();
input.value="";
}
}
//enter key press listener
function addOnKey(event) {
if (inputLength() > 0 && event.keyCode === 13) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
var div = document.createElement("div");
ul.appendChild(div);
div.appendChild(li);
div.setAttribute("class", "listCont");
abcd();
addLiEvent();
input.value="";
}
}
//"done" controller
function toggleList(event) {
event.target.classList.toggle("done");
}
//"X" button creation
function abcd() {
var del = document.createElement("button");
del.appendChild(document.createTextNode("X"));
document.getElementsByTagName("li")[j].appendChild(del);
del.setAttribute("class", "bd");
j++;
}
//delete li on DELETE button click
var deleteListItem = function() {
// Selects button's parent node, "the li"
var liItem = event.target.parentNode;
// Selects the li's parent node, "the div"
var liParent = liItem.parentNode;
// Selects the div's parent node
var divParent = liParent.parentNode;
// Removed the ul's child node, the li
divParent.removeChild(liParent);
j--;
}
button.addEventListener("click", addOnMouse);
input.addEventListener("keydown", addOnKey);
list2.addEventListener("click", toggleList);