-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
63 lines (48 loc) · 1.67 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
62
63
const addButton = document.getElementById("add-button");
const textInput = document.getElementById("text-input");
const list = document.getElementById("lists");
// Function adds a new item to the list
function addItem() {
const text = textInput.value;
const li = document.createElement("li");
li.innerText = text;
li.classList.add("editable");
list.appendChild(li);
textInput.value = "";
}
// Function removes the last item from a list
function removeItem() {
const items = list.getElementsByTagName("li");
const lastItem = items[items.length - 1];
lastItem.remove();
}
// Function changes the txt of the selected item on the page
function changeText() {
const editableItem = document.querySelector(".editable.active");
if (editableItem) {
editableItem.innerText = textInput.value;
}
}
// Function that changes te styles of the selected element on the the page
function changeStyle() {
const items = list.getElementsByTagName("li");
const selectedItem = event.target;
selectedItem.style.color = "rgb(255, 0, 0)";
selectedItem.style.backgroundColor = "#ffff00";
}
addButton.addEventListener("click", addItem);
document.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
addItem();
}
});
list.addEventListener("click", (event) => {
const editableItem = event.target.closest(".editable");
if (editableItem){
document.querySelector(".editable.active")?.classList.remove("active");
editableItem.classList.add("active");
}
});
textInput.addEventListener("input", changeText);
list.addEventListener("dblclick", removeItem);
list.addEventListener("click", changeStyle);