-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
53 lines (45 loc) · 1.22 KB
/
index.ts
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
import { v4 as uuidv4 } from "uuid";
type Task = {
id: string;
title: string;
completed: boolean;
createdAt: Date;
};
const list = document.querySelector<HTMLUListElement>(
"#list"
) as HTMLUListElement;
const form = document.querySelector<HTMLFormElement>(
"#new-task-form"
) as HTMLFormElement;
const input = document.querySelector<HTMLInputElement>(
"#new-task-title"
) as HTMLInputElement; // Corrected selector
const tasks: Task[] = [];
form?.addEventListener("submit", (e) => {
e.preventDefault();
if (input?.value == "" || input?.value == null) return;
const newTask: Task = {
id: uuidv4(),
title: input.value,
completed: false,
createdAt: new Date(),
};
tasks.push(newTask);
input.value = " ";
addListItem(newTask);
input.value = "";
});
function addListItem(task: Task) {
const item = document.createElement("li");
const label = document.createElement("label");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = task.completed;
checkbox.addEventListener("change", () => {
task.completed = checkbox.checked;
console.log(tasks);
});
label.append(checkbox, task.title);
item.append(label);
list?.append(item);
}