-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
148 lines (89 loc) · 3.52 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// const item = document.getElementById("item")
// const todobox = document.getElementById("to-do-box");
// //const tag = document.getElementById("cross");
// //const checkbox = document.getElementById("tick");
// item.addEventListener("keyup",function(event)
// {
// if (event.key == "Enter")
// {
// addtodo(this.value);
// this.value = "";
// }
// }
// );
// const addtodo = function(item)
// {
// const listItem =document.createElement("li");
// //new
// listItem.innerHTML = `
// ${item}
// <input type="checkbox" id="tick">
// <i class="fa-solid fa-xmark"></i>
// `
// //other way of doing it(without using `${item}`) is '\n' + item + '\n<i class="fa-solid fa-xmark"></i>\n'
// todobox.appendChild(listItem);
// listItem.querySelector("i").addEventListener("click",function()
// {
// todobox.removeChild(listItem);
// })
// }
// function saveData() {
// localStorage.setItem("data",todobox.innerHTML);
// }
// function display(){
// todobox.innerHTML = localStorage.getItem("data");
// }
var item = document.getElementById("item");
var todobox = document.getElementById("to-do-box");
var button = document.getElementById("btn");
button.addEventListener("click", function(event) {
var inputValue = item.value; // Get the value of the input element
addtodo(inputValue);
item.value ="";
//console.log(item.value);
});
const addtodo = function(item)
{
if (item.trim() === "") {
alert("Empty text can't be added");
return; // Exit the function if the item is empty
}
const listItem = document.createElement("li");
listItem.innerHTML = ` <span class="editable-text">${item}</span>
<input type="checkbox">
<button>Edit</button>
<i class="fa-solid fa-xmark"></i>
`
todobox.appendChild(listItem);
//todobox.contentEditable(listItem);
// listItem.querySelector("button").addEventListener("click",function()
// {
// this.innerHTML = "save";
// })
const textElement = listItem.querySelector(".editable-text");
listItem.querySelector("button").addEventListener("click", function() {
if (this.innerHTML === "Edit") {
// Change button text to "Save"
this.innerHTML = "Save";
// Make the text content editable
textElement.contentEditable = true;
// Focus on the editable text
textElement.focus();
} else if (this.innerHTML === "Save") {
// Change button text back to "Edit"
this.innerHTML = "Edit";
// Make the text content non-editable
textElement.contentEditable = false;
// Update the text content with the edited value
textElement.textContent = textElement.textContent.trim();
}
});
listItem.querySelector("input").addEventListener("click",function()
{
this.parentElement.classList.toggle("done");
})
listItem.querySelector("i").addEventListener("click",function()
{
todobox.removeChild(listItem);
});
}