-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
117 lines (108 loc) · 3.65 KB
/
Copy pathapp.js
File metadata and controls
117 lines (108 loc) · 3.65 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//Variables
const addBut = document.querySelector('.add-button');
let todoBox = document.querySelector('.todo-box');
const todoContainer = document.querySelector('.todo-container');
//Event Listeners
addBut.addEventListener('click', createTodo);
todoContainer.addEventListener('click', deleteCheck);
document.addEventListener('DOMContentLoaded', getTodos);
//Functions
function createTodo(event) {
//Prevent the page from submitting
event.preventDefault();
const todoValue = todoBox.value;
console.log(todoValue);
//Creating LI
const todoItem = document.createElement('li');
todoItem.classList.add('todo-item');
todoContainer.appendChild(todoItem);
//Add Todo
saveLocalTodos(todoBox.value);
//Creating the Text
const todoInput = document.createElement('p');
todoInput.innerText = todoBox.value;
todoInput.classList.add('todo-input');
todoItem.appendChild(todoInput);
//Clearing the input box
todoBox.value = '';
//Creating Check Button
const checkBut = document.createElement('button');
checkBut.classList.add('check-button');
checkBut.innerHTML = '<img src="img/icons/check.svg" >';
todoItem.appendChild(checkBut);
//Creating Trash Button
const trashBut = document.createElement('button');
trashBut.innerHTML = '<img src="img/icons/trash.svg" >';
trashBut.classList.add('trash-button');
todoItem.appendChild(trashBut);
}
function deleteCheck(e) {
const item = e.target;
//Delete TODO
if (item.classList[0] === "trash-button") {
const todo = item.parentElement;
todo.classList.add('fall');
removeTodo(todo);
todo.addEventListener('transitionend', function() {
todo.remove();
})
}
//Check TODO
if (item.classList[0] === "check-button") {
const todo = item.parentElement;
todo.classList.toggle('completed');
}
}
function saveLocalTodos(todo) {
//TO CHECK IF I ALREADY HAVE
let todos;
if (localStorage.getItem('todos') === null) {
todos = [];
} else {
todos = JSON.parse(localStorage.getItem('todos'));
}
todos.push(todo);
localStorage.setItem('todos', JSON.stringify(todos))
}
function getTodos() {
//TO CHECK IF I ALREADY HAVE
let todos;
if (localStorage.getItem('todos') === null) {
todos = [];
} else {
todos = JSON.parse(localStorage.getItem('todos'));
}
todos.forEach(function(todo) {
//Creating LI
const todoItem = document.createElement('li');
todoItem.classList.add('todo-item');
todoContainer.appendChild(todoItem);
//Creating the Text
const todoInput = document.createElement('p');
todoInput.innerText = todo;
todoInput.classList.add('todo-input');
todoItem.appendChild(todoInput);
//Clearing the input box
//Creating Check Button
const checkBut = document.createElement('button');
checkBut.classList.add('check-button');
checkBut.innerHTML = '<img src="img/icons/check.svg" >';
todoItem.appendChild(checkBut);
//Creating Trash Button
const trashBut = document.createElement('button');
trashBut.innerHTML = '<img src="img/icons/trash.svg" >';
trashBut.classList.add('trash-button');
todoItem.appendChild(trashBut);
})
}
function removeTodo(todo) {
let todos;
if (localStorage.getItem('todos') === null) {
todos = [];
} else {
todos = JSON.parse(localStorage.getItem('todos'));
}
const todoIndex = todo.children[0].innerText;
todos.splice(todos.indexOf(todoIndex), 1);
localStorage.setItem('todos', JSON.stringify(todos));
}