-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (77 loc) · 2.68 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
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
const listElement = document.querySelector('.todo-list');
const inputElement = document.querySelector('input');
const addButtonElement = document.querySelector('.add-todo');
inputElement.focus();
inputElement.select();
const todos = JSON.parse(localStorage.getItem('list_todos')) || [];
function renderTodos() {
listElement.innerHTML = '';
for (todo of todos) {
const todoElement = document.createElement('li');
todoElement.classList.add('todo-element');
const todoText = document.createTextNode(todo + ' ');
const checkElement = document.createElement('a');
// checkElement.setAttribute('href', '#');
checkElement.classList.add('check-element');
const checkIcon = document.createTextNode('✅');
checkElement.appendChild(checkIcon);
const pos = todos.indexOf(todo);
checkElement.setAttribute('onclick', 'deleteTodo(' + pos + ')');
todoElement.appendChild(todoText);
todoElement.appendChild(checkElement);
listElement.appendChild(todoElement);
}
}
renderTodos();
function addTodo() {
const todoText = inputElement.value;
if (!todoText) {
return;
}
todos.push(todoText);
inputElement.value = '';
renderTodos();
saveToStorage();
}
addButtonElement.onclick = addTodo;
function enterAsClick(event) {
if (event.keyCode === 13) {
event.preventDefault(); // Cancel the default action, if needed
addButtonElement.click(); // Trigger the button element with a click
}
}
inputElement.addEventListener('keyup', enterAsClick);
function deleteTodo(pos) {
todos.splice(pos, 1);
renderTodos();
saveToStorage();
}
function setDarkTheme() {
const body = document.querySelector('body'),
app = document.querySelector('#app'),
h1 = document.querySelector('.title'),
input = document.querySelector('input'),
motivational = document.querySelector('.motivational-phrase'),
emojiRun = document.querySelector('.emoji-run-element'),
themeButtonElement = document.querySelector('.theme-selector'),
addTodoButton = document.querySelector('.add-todo');
body.classList.toggle('dark-theme');
app.classList.toggle('dark-theme');
h1.classList.toggle('dark-theme');
input.classList.toggle('dark-theme');
motivational.classList.toggle('dark-theme');
emojiRun.classList.toggle('dark-theme');
themeButtonElement.classList.toggle('dark-theme');
addTodoButton.classList.toggle('dark-theme');
if (app.classList.contains('dark-theme')) {
localStorage.setItem('dark-theme', 'enabled');
} else {
localStorage.setItem('dark-theme', 'disabled');
}
}
if (localStorage.getItem('dark-theme') === 'enabled') {
setDarkTheme();
}
function saveToStorage() {
localStorage.setItem('list_todos', JSON.stringify(todos));
}