forked from spykelionel/simple-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (56 loc) · 1.97 KB
/
app.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
let item = document.getElementById("item");
let button = document.getElementById("add");
let ul = document.getElementById("mylist");
const addItem = (e) => {
let li = document.createElement("li");
let deleteButton = document.createElement("button");
let editButton = document.createElement("button");
let saveButton = document.createElement("button");
let div = document.createElement("div");
let input = document.createElement("input");
deleteButton.setAttribute("id", "delete-me");
deleteButton.classList.add("button");
editButton.setAttribute("id", "edit-me");
editButton.classList.add("button");
saveButton.setAttribute("id", "save-me");
saveButton.classList.add("button");
div.classList.add("some-item");
input.setAttribute("placeholder", "Enter new item");
deleteButton.textContent = "delete";
editButton.textContent = "edit";
saveButton.textContent = "Save";
li.textContent = item.value;
item.value = "";
const saveMe = (e) => {
li.textContent = input.value;
div.removeChild(input);
div.removeChild(saveButton);
div.appendChild(editButton);
div.appendChild(deleteButton);
li.appendChild(div);
};
const editMe = (e) => {
const textNodes = Array.from(li.childNodes).filter(
(node) => node.nodeType === Node.TEXT_NODE
);
const prevText = textNodes.map((node) => node.textContent.trim()).join("");
item.value = "";
input.value = prevText;
input.focus();
input.select();
div.appendChild(input);
div.appendChild(saveButton).addEventListener("click", saveMe);
};
const deleteMe = (e) => {
ul.removeChild(li);
};
div.appendChild(editButton).addEventListener("click", editMe);
div.appendChild(deleteButton).addEventListener("click", deleteMe);
li.appendChild(div);
ul.appendChild(li).appendChild(div);
div.style.display = "inline";
};
button.addEventListener("click", addItem);
function load() {
item.focus();
}