-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (49 loc) · 1.38 KB
/
Copy pathscript.js
File metadata and controls
69 lines (49 loc) · 1.38 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
var button = document.getElementById("enter");
var input = document.getElementById("userInput");
var ul = document.querySelector("ul");
var clear = document.getElementById("clearList");
// CHECK IF THERE IS INPUT
function inputLength () {
return input.value.length;
}
// ADD ITEMS TO THE LIST
function addListElement() {
// item + event listener to cross off
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value));
ul.appendChild(li);
input.value = "";
li.addEventListener("click", crossOff);
// delete button
var delButton = document.createElement("button");
delButton.appendChild(document.createTextNode("Delete"));
li.appendChild(delButton);
delButton.addEventListener("click", removeParent);
}
// ADD ITEM ON BUTTON CLICK
function addElementOnClick() {
if (inputLength() > 0) {
addListElement();
}
}
// ADD ITEM ON ENTER
function addElementOnKey(e) {
if (inputLength() > 0 && e.which === 13) {
addListElement();
}
}
// REMOVE ITEM ON DELETE CLICK
function removeParent(p) {
p.target.parentElement.remove();
}
// CROSS OFF ITEM ON MOUSE CLICK
function crossOff(l) {
l.target.classList.toggle("done");
}
// CLEARING LIST
function ItemsOff() {
ul.replaceChildren("");
}
button.addEventListener("click", addElementOnClick);
input.addEventListener("keypress", addElementOnKey);
clear.addEventListener("click", ItemsOff);