-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (43 loc) · 1.25 KB
/
script.js
File metadata and controls
53 lines (43 loc) · 1.25 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
let submit = document.getElementById("submit");
let addtask = document.getElementById("addtask");
let pend = document.getElementById("pend");
let donelist = document.getElementById("complete");
let list = [];
function addtaskf() {
let taskText = addtask.value.trim();
if (taskText !== "") {
list.push(taskText);
addPendingTask(list.length - 1, taskText);
addtask.value = "";
}
}
function addPendingTask(id, text) {
let li = document.createElement("li");
li.id = `pending-${id}`;
let btn = document.createElement("button");
btn.textContent = text;
btn.onclick = function () {
moveToCompleted(id);
};
li.appendChild(btn);
pend.appendChild(li);
}
function moveToCompleted(id) {
let taskText = list[id];
let li = document.createElement("li");
li.id = `completed-${id}`;
li.textContent = taskText;
li.onclick = function () {
li.remove();
};
donelist.appendChild(li);
let taskElement = document.getElementById(`pending-${id}`);
if (taskElement) taskElement.remove();
}
submit.addEventListener('click', addtaskf);
addtask.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
addtaskf();
}
});