-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
54 lines (39 loc) · 1.22 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
var button = document.getElementById("enter");
var input = document.getElementById("userInput");
var ul = document.querySelector("ul");
var globalVariables = {}
function inputLength(){
return input.value.length;
}
function createListElement(){
li = document.createElement("li");
globalVariables.delete_btn = document.createElement("button");
globalVariables.delete_btn.textContent = "Delete";
li.appendChild(document.createTextNode(input.value));
li.appendChild(globalVariables.delete_btn);
ul.appendChild(li);
input.value = "";
}
function deleteList(event){
event.target.parentElement.outerHTML = "";
}
function addListAfterKeypress(event){
if (((inputLength()>0) && (event.which === 13))){
createListElement();
globalVariables.delete_btn.addEventListener("click", deleteList);
}
}
function addListAfterClick(){
if (inputLength()>0){
createListElement();
globalVariables.delete_btn.addEventListener("click", deleteList);
}
}
function toggleItem(event) {
if (event.target.tagName === "LI") {
event.target.classList.toggle("done");
}
}
input.addEventListener("keypress", addListAfterKeypress);
ul.addEventListener("click", toggleItem);
button.addEventListener("click", addListAfterClick);