-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (52 loc) · 1.45 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
var button = document.getElementById("enter");
var input = document.getElementById("userinput");
var ul = document.querySelector("ul");
var count = 0;
var itemList = {};
button.addEventListener("click", addListAfterClick);
input.addEventListener("keypress", addListAfterKeypress);
function inputLength() {
return input.value.length;
}
function createListElement() {
var id = count++;
var delBtn = createDelBtn(id);
var li = createLi();
itemList[`${id}`] = li;
ul.appendChild(li);
li.appendChild(delBtn);
input.value = "";
}
function createLi(id) {
var newElement = document.createElement("li");
var textNode = document.createElement("span");
textNode.appendChild(document.createTextNode(input.value));
newElement.appendChild(textNode);
textNode.addEventListener("click", changeClass);
return newElement;
}
function createDelBtn(id) {
var delBtn = document.createElement('button');
delBtn.innerHTML = "delete";
delBtn.setAttribute("listId", `${id}`);
delBtn.addEventListener("click", deleteListElement);
return delBtn;
}
function addListAfterClick() {
if (inputLength() > 0) {
createListElement();
}
}
function addListAfterKeypress(event) {
if (inputLength() > 0 && event.keyCode === 13) {
createListElement();
}
}
function changeClass(event) {
event.target.classList.toggle("done");
}
function deleteListElement(event) {
const id = parseInt(event.target.getAttribute("listId"));
itemList[`${id}`].remove();
itemList[`${id}`] = undefined;
}