-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (84 loc) · 2.87 KB
/
script.js
File metadata and controls
96 lines (84 loc) · 2.87 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const inputBox = document.getElementById("taskInput");
const listContainer = document.getElementById("taskList");
function addTask() {
const taskText = inputBox.value.trim();
if (taskText === "") {
alert("You must write something!");
return;
}
const tasks = listContainer.getElementsByTagName("li");
console.log(tasks);
for (let i = 0; i < tasks.length; i++) {
if (tasks[i].childNodes[0].nodeValue.trim() === taskText) {
alert("Task already exists!");
return;
}
}
let li = document.createElement("li");
li.textContent = taskText;
const editBtn = document.createElement("button");
editBtn.textContent = "✏️";
editBtn.className = "editBtn";
editBtn.onclick = function () {
const currentText = li.firstChild.nodeValue.trim();
const newText = prompt("Edit your task:", currentText);
if (newText && newText.trim() !== "") {
const trimmedNewText = newText.trim();
//existing logic for checking duplicates
for (let i = 0; i < tasks.length; i++) {
const otherText = tasks[i].childNodes[0].nodeValue.trim();
if (tasks[i] !== li && otherText === trimmedNewText) {
alert("Task already exists!");
return;
}
}
li.firstChild.nodeValue = trimmedNewText;
saveData();
}
};
li.appendChild(editBtn);
const deleteBtn = document.createElement("span");
deleteBtn.innerHTML = "\u00d7";
deleteBtn.onclick = function () {
li.remove();
saveData();
};
li.appendChild(deleteBtn);
listContainer.appendChild(li);
inputBox.value = "";
setAlertMessage("Todo item Created Successfully!");
saveData();
//li.innerHTML = inputBox.value + ' <button class="deleteBtn" onclick="deleteTask(this)">Delete</button>';
// li.innerHTML = inputBox.value;
// listContainer.appendChild(li);
// let span = document.createElement("span");
// span.innerHTML = "\u00d7";
// li.appendChild(span);
// inputBox.value = ""; // Clear the input box after adding a task
// setAlertMessage("Todo item Created Successfully!");
// saveData(); // Save the current state of the list to local storage
}
listContainer.addEventListener(
"click",
function (e) {
if (e.target.tagName === "LI") {
e.target.classList.toggle("checked");
saveData();
}
// else if (e.target.tagName === "SPAN") {
// e.target.parentElement.remove();
// saveData();
// }
},
false
);
function saveData() {
localStorage.setItem("data", listContainer.innerHTML);
}
function showTask() {
listContainer.innerHTML = localStorage.getItem("data");
}
showTask();
function setAlertMessage(message) {
console.log(message);
}