-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
98 lines (79 loc) 路 2.63 KB
/
Copy pathapp.js
File metadata and controls
98 lines (79 loc) 路 2.63 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const addBtn = document.getElementById("todo-button");
const todoInput = document.getElementById("todo-input");
const todoUl = document.getElementById("todo-ul");
const alert = document.querySelector(".alertmsg");
const today = document.querySelector(".today");
let todos = JSON.parse(localStorage.getItem("TODOS")) || [];
console.log(todos);
const renderSavedTodos = () =>{
todos.forEach((todo) => {
createListElement(todo);
});
};
renderSavedTodos();
addBtn.addEventListener("click",function(){
if(!todoInput.value){
alert.innerText = `Please enter an item!`;
setTimeout(()=>{
alert.innerText=""
},5000);
}
else{
const newTodo = {
id: new Date().getTime(),
completed: false,
text: todoInput.value,
};
createListElement(newTodo);
todos.push(newTodo);
localStorage.setItem("TODOS",JSON.stringify(todos));
console.log(todos);
todoInput.value = "";
}
});
function createListElement(newTodo){
const{ id , completed , text } = newTodo;
const li = document.createElement("li");
li.setAttribute("id",id);
completed && li.classList.add("checked");
const okIcon= document.createElement("i");
okIcon.setAttribute("class","fas fa-check");
li.appendChild(okIcon);
const p = document.createElement("p");
const pTextNode = document.createTextNode(text);
p.appendChild(pTextNode);
li.appendChild(p);
const deleteIcon = document.createElement("i");
deleteIcon.setAttribute("class","fas fa-trash");
li.appendChild(deleteIcon);
console.log(li);
todoUl.appendChild(li);
}
todoUl.addEventListener("click",(e)=>{
console.log(e.target);
const id = e.target.parentElement.getAttribute("id");
if(e.target.classList.contains("fa-trash")){
e.target.parentElement.remove();
todos=todos.filter((todo)=>todo.id !==Number(id));
localStorage.setItem("TODOS",JSON.stringify(todos));
} else if (e.target.classList.contains("fa-check")){
e.target.parentElement.classList.toggle("checked");
todos.map((todo, index)=>{
if(todo.id == id){
todos[index].completed = !todos[index].completed;
}
});
console.log(todos);
localStorage.setItem("TODOS",JSON.stringify(todos));
}
});
todoInput.addEventListener("keydown",(e)=>{
if(e.code === "Enter"){
addBtn.click();
}
});
window.onload = function (){
var newDate = new Date();
today.innerText=`Today: ${newDate.getDate()}.${newDate.getMonth()+1}.${newDate.getFullYear()}`;
todoInput.focus();
};