-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
127 lines (115 loc) · 3.41 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//Declare variables
var button = document.getElementById("enter");
var input = document.getElementById("userinput");
var ul = document.getElementById("ul");
var ClearAll = document.getElementById("clear");
var listItems = document.getElementsByClassName("list-group-item");
//returns length of list
function listLength() {
return listItems.length;
}
//returns length of user input
function inputLength() {
return input.value.length;
}
//creates a new list element & clears the search bar
function createListElement() {
var li = document.createElement("li");
li.className = "list-group-item list-group-item-info";
li.setAttribute("id", "li");
var cbox = document.createElement("input");
cbox.setAttribute("type", "checkbox");
cbox.className = "checkbox";
li.appendChild(cbox);
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
input.value = "";
var delbtn = document.createElement("i");
delbtn.className = "far fa-trash-alt float-right";
delbtn.setAttribute("id", "del");
li.appendChild(delbtn);
delbtn.onclick = removeParent;
}
//adds element once button clicked
function addListAfterClick() {
if (inputLength() > 0) {
createListElement();
}
}
// creates element once they press enter
function addListAfterKeyPress(event) {
if (inputLength() > 0 && event.keyCode == 13) {
createListElement();
}
}
//creates delete buttons & checkbox for existing items
function deleteButton() {
var delbtn = document.createElement("i");
delbtn.className = "far fa-trash-alt float-right";
delbtn.setAttribute("id", "del");
var cbox = document.createElement("input");
cbox.setAttribute("type", "checkbox");
cbox.className = "checkbox";
//Adds to existing list
listItems[i].insertBefore(cbox, listItems[i].firstChild);
listItems[i].appendChild(delbtn);
delbtn.onclick = removeParent;
}
//runs to add delete buttons to existing items
for (i = 0; i < listLength(); i++) {
deleteButton();
}
//removes parent node. First checks if parent node is <li> and if so, removes parent. Otherwise it will be <s> so removes grandparent <li>
function removeParent(evt) {
console.log(evt.target.parentElement.getAttribute("id") == "li");
if (evt.target.parentElement.getAttribute("id") == "li") {
evt.target.parentElement.remove();
} else {
evt.target.parentElement.parentElement.remove();
}
}
//deletes all list elements on click
ClearAll.onclick = function(evt) {
while (ul.firstChild) {
ul.removeChild(ul.firstChild);
}
};
//deletes last item by using 'del' key
function delitem(evt) {
if (evt.keyCode == 46) ul.removeChild(ul.lastElementChild);
}
button.addEventListener("click", addListAfterClick);
input.addEventListener("keydown", addListAfterKeyPress);
document.addEventListener("keydown", delitem);
//changes strikethrough on list
$(document).on("change", ".checkbox", function() {
if (
$(this)
.parent()
.is("s")
) {
$(this).unwrap();
} else {
$(this)
.parent()
.wrapInner("<s id='s'></s>");
}
});
//removes parent node. First checks if parent node is <li> and if so, removes parent. Otherwise it will be <s> so removes grandparent <li>
// $(document).on("click", "#del", function() {
// console.log($(this));
// if (
// $(this)
// .parent()
// .is($("li"))
// ) {
// $(this)
// .parent()
// .remove();
// } else {
// $(this)
// .parent()
// .parent()
// .remove();
// }
// });