-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
62 lines (56 loc) · 2.02 KB
/
main.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
const addButton = document.getElementById('add-button');
const todoInput = document.getElementById('todo-input');
const todoList = document.getElementById('todo-list');
const modeBtn = document.querySelector('.mode');
const htmlElement = document.querySelector('html');
let currentTheme = htmlElement.getAttribute('data-theme');
const storedTheme = localStorage.getItem('toDoTheme');
if (storedTheme) {
currentTheme = storedTheme;
htmlElement.setAttribute('data-theme', currentTheme);
updateIcons(currentTheme);
}
function themeToggle() {
currentTheme = currentTheme === 'dark' ? 'light' : 'dark';
htmlElement.setAttribute('data-theme', currentTheme);
localStorage.setItem('toDoTheme', currentTheme);
updateIcons(currentTheme);
}
function updateIcons(theme) {
const moonIcon = theme === 'dark' ? 'none' : 'inline';
const sunIcon = theme === 'light' ? 'none' : 'inline';
const moonIconElement = document.querySelector('.fa-moon');
const sunIconElement = document.querySelector('.fa-sun');
moonIconElement.style.display = moonIcon;
sunIconElement.style.display = sunIcon;
}
function handleButtons() {
const todoText = todoInput.value.trim();
if (todoText !== '') {
const todoItem = document.createElement('li');
todoItem.innerHTML = `
<div class="todo-content"><p>${todoText}</p></div>
<button class="delete-button">Delete</button>
`;
const deleteButton = todoItem.querySelector('.delete-button');
deleteButton.addEventListener('click', () => {
todoItem.remove();
localStorage.removeItem('todos');
});
todoList.appendChild(todoItem);
localStorage.setItem('todos', todoList.innerHTML);
todoInput.value = '';
}
}
addButton.addEventListener('click', handleButtons);
todoInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
handleButtons();
}
});
modeBtn.addEventListener('click', themeToggle);
// Restore todos from local storage on page load
const storedTodos = localStorage.getItem('todos');
if (storedTodos) {
todoList.innerHTML = storedTodos;
}