-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
61 lines (55 loc) · 1.65 KB
/
index.ts
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
const formBtn = document.querySelector(".form__btn");
const ul = document.querySelector(".section__ul");
formBtn?.addEventListener("click", () => {
let inputTxt = getInputValue();
if (typeof inputTxt === "string") {
const item = createItem(inputTxt);
ul?.appendChild(item);
}
ul?.addEventListener("click", (event) => {
if (event.target instanceof HTMLElement) {
const id = event.target.dataset.id;
const toBeDeleted = document.querySelector(
`.section__li[data-id="${id}"]`
);
console.log(toBeDeleted);
if (event.target.tagName === "I") {
toBeDeleted?.remove();
}
}
});
});
ul?.addEventListener("click", (event) => {
if (event.target instanceof HTMLElement) {
const span = event.target.closest("span");
span?.classList.contains("selected")
? span?.classList.remove("selected")
: span?.classList.add("selected");
}
});
function getInputValue(): string | undefined {
const inputTag = document.querySelector(".form__input");
if (inputTag instanceof HTMLInputElement) {
if (inputTag.value === "") {
inputTag.focus();
} else {
let result = inputTag.value;
inputTag.value = "";
return result;
}
}
}
let id: number = 0;
function createItem(text: string): HTMLLIElement {
const itemRow = document.createElement("li");
itemRow.setAttribute("class", "section__li");
itemRow.setAttribute("data-id", id.toString());
itemRow.innerHTML = `
<span class="span item__name" data-id=${id}>${text}</span>
<button class="item__delete btn">
<i class="fa-solid fa-trash" data-id=${id}></i>
</button>
`;
id++;
return itemRow;
}