-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
41 lines (37 loc) · 1.11 KB
/
todo.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
const todoList = JSON.parse(localStorage.getItem("todoList")) || [
{ Text: "make dinner", date: "2022-12-22" },
{ Text: "wash dishes", date: "2021-12-21" },
];
renderTodoList();
function renderTodoList() {
let todoListHTML = "";
/* for (let i = 0; i < todoList.length; i++) {
const todo = todoList[i]; */
todoList.forEach((todo,i)=>{
const html = `<p><li>${todo.Text} ${todo.date} <button onclick='upDate(${i});
'>Delete</button></li></p>`;
todoListHTML += html;
});
document.querySelector("div").innerHTML = todoListHTML;
}
function keyDown(event) {
if (event.key === "Enter") {
pushTodo();
}
}
function upDate(i) {
todoList.splice(i, 1);
renderTodoList();
localStorage.setItem("todoList", JSON.stringify(todoList));
}
function pushTodo() {
const inputText = document.querySelector(".js-text-input");
const inputDate = document.querySelector(".js-date-input");
const date = inputDate.value;
const Text = inputText.value;
todoList.push({ Text, date });
inputDate.value = "";
inputText.value = "";
renderTodoList();
localStorage.setItem("todoList", JSON.stringify(todoList));
}