-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (43 loc) · 1.56 KB
/
script.js
File metadata and controls
48 lines (43 loc) · 1.56 KB
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
function addTask() {
const taskInput = document.getElementById("taskInput");
const taskText = taskInput.value.trim();
if (taskText === "") return;
const taskList = document.getElementById("taskList");
const li = document.createElement("li");
li.innerHTML = `
<span></span> <span>${taskText}</span>
<button onclick="markDone(this)">Done</button>
<button onclick="editTask(this)">Edit</button>
<button onclick="deleteTask(this)">Delete</button>
`;
taskList.appendChild(li);
taskInput.value = "";
updateTaskNumbers();
}
function markDone(button) {
const taskItem = button.parentElement;
taskItem.querySelector("span:nth-child(2)").classList.toggle("done");
}
function editTask(button) {
const taskItem = button.parentElement;
const taskText = taskItem.querySelector("span:nth-child(2)");
const input = document.createElement("input");
input.type = "text";
input.value = taskText.innerText;
taskItem.replaceChild(input, taskText);
input.focus();
input.addEventListener("blur", function() {
taskText.innerText = input.value;
taskItem.replaceChild(taskText, input);
});
}
function deleteTask(button) {
button.parentElement.remove();
updateTaskNumbers();
}
function updateTaskNumbers() {
const taskList = document.getElementById("taskList").children;
for (let i = 0; i < taskList.length; i++) {
taskList[i].querySelector("span:first-child").innerText = (i + 1) + ". ";
}
}