-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (55 loc) · 1.67 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
const textarea = document.querySelector('textarea')
const addBtn = document.getElementById('addBtn')
const todoContainer = document.querySelector('.todoContainer')
let todoList = []
function initialLoad() {
if (!localStorage.getItem('todos')) { return }
todoList = JSON.parse(localStorage.getItem('todos')).todoList
updateUI()
}
initialLoad()
function addTodo() {
const todo = textarea.value
if (!todo) { return }
console.log('Added todo: ', todo)
todoList.push(todo)
textarea.value = '' // resets to empty
updateUI()
}
function editTodo(index) {
textarea.value = todoList[index]
todoList = todoList.filter((element, elementIndex) => {
if (index === elementIndex) { return false }
return true
})
updateUI()
}
function deleteTodo(index) {
todoList = todoList.filter((element, elementIndex) => {
if (index === elementIndex) { return false }
return true
})
updateUI()
}
function updateUI() {
let newInnerHTML = ''
todoList.forEach((todoElement, todoIndex) => {
newInnerHTML += `
<div class="todo">
<p>${todoElement}</p>
<div class="btnContainer">
<button class="iconBtn" onclick="editTodo(${todoIndex})">
<i class="fa-solid fa-pen-to-square"></i>
</button>
<button class="iconBtn" onclick="deleteTodo(${todoIndex})">
<i class="fa-solid fa-xmark"></i>
</button>
</div>
</div>
`
})
todoContainer.innerHTML = newInnerHTML
// to save to localstorage
localStorage.setItem('todos', JSON.stringify({ todoList }))
}
addBtn.addEventListener('click', addTodo)