-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (49 loc) · 1.42 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
const todoArray = JSON.parse(localStorage.getItem("todoArray")) || [
{
name: "make dinner",
dueDate: "2022-12-22",
},
{
name: "wash dishes",
dueDate: "2022-12-22",
},
];
todoDisplay();
document.querySelector(".js-add-todo-button").addEventListener("click", () => {
todoList();
});
function todoList() {
let nameInputElement = document.querySelector(".js-todo-input-name");
const name = nameInputElement.value;
let dateInputElement = document.querySelector(".js-todo-input-date");
const dueDate = dateInputElement.value;
todoArray.push({ name: name, dueDate: dueDate });
console.log(todoArray);
nameInputElement.value = "";
todoDisplay();
saveToStorage();
}
function todoDisplay() {
let todoListHTML = "";
todoArray.forEach((todoObject, index) => {
const { name, dueDate } = todoObject;
const html = `<div>${name}</div>
<div> ${dueDate} </div>
<button class="delete-todo-button js-delete-todo-button"
>delete</button>`;
todoListHTML += html;
console.log(index);
});
document.querySelector(".js-todo-display").innerHTML = todoListHTML;
document
.querySelectorAll(".js-delete-todo-button")
.forEach((deleteButton, index) => {
deleteButton.addEventListener("click", () => {
todoArray.splice(index, 1);
todoDisplay();
});
});
}
function saveToStorage() {
localStorage.setItem("todoArray", JSON.stringify(todoArray));
}