-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (40 loc) · 1.2 KB
/
app.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
// Todos Array
let todos = [];
// Grabbing Elements
let todoList = document.getElementById("todo-list");
let todoInput = document.getElementById("new-todo");
let addTodoBtn = document.getElementById("add-todo");
function addTodo() {
// Create a new todo
let todoID = `todo-${todos.length}`;
let todo = {
id: todoID,
content: todoInput.value,
done: false,
created_at: new Date(),
};
// adding it to the todos array
todos.push(todo);
let todoTemplate = document.getElementById("todo-template");
// Creating todo element by cloning the template
todoHTML = todoTemplate.content.cloneNode(true);
todoHTML.id = todoID;
todoHTML.querySelector("p").innerText = todo.content;
let todoChecked = todoHTML.querySelector("input");
todoList.appendChild(todoHTML);
// check the todo
todoChecked.addEventListener("change", function () {
todo.done = !todo.done;
if (todo.done) {
todoHTML.classList.add("line-through");
} else {
todoHTML.classList.remove("line-through");
}
});
// clear the input
todoInput.value = "";
}
addTodoBtn.addEventListener("click", () => {
console.log(todoInput.value.length);
if (todoInput.value.length > 0) addTodo();
});