-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (59 loc) · 2.12 KB
/
script.js
File metadata and controls
65 lines (59 loc) · 2.12 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
// ... existing code ...
const todoInput = document.getElementById('todoInput');
const addBtn = document.getElementById('addBtn');
const todoList = document.getElementById('todoList');
const themeBtn = document.getElementById('themeBtn');
const themeOptions = document.getElementById('themeOptions');
const themeOptionBtns = themeOptions.querySelectorAll('.theme-option');
const themes = ['', 'theme-2', 'theme-3', 'theme-4'];
let currentTheme = 0;
function renderTodo(text, done = false) {
const li = document.createElement('li');
li.className = 'todo-item' + (done ? ' done' : '');
li.innerHTML = `
<input type="checkbox" class="todo-checkbox"${done ? ' checked' : ''}>
<span>${text}</span>
<button class="delete-btn" title="Delete">×</button>
`;
const checkbox = li.querySelector('.todo-checkbox');
checkbox.onclick = () => {
li.classList.toggle('done');
};
li.querySelector('span').onclick = () => {
checkbox.checked = !checkbox.checked;
li.classList.toggle('done');
};
li.querySelector('.delete-btn').onclick = () => {
li.classList.add('removing');
setTimeout(() => li.remove(), 300);
};
todoList.appendChild(li);
}
addBtn.onclick = () => {
const value = todoInput.value.trim();
if (value) {
renderTodo(value);
todoInput.value = '';
todoInput.focus();
}
};
todoInput.addEventListener('keydown', e => {
if (e.key === 'Enter') addBtn.onclick();
});
// Show/hide theme options on button click
themeBtn.onclick = () => {
themeOptions.style.display = themeOptions.style.display === 'none' ? 'flex' : 'none';
};
// Theme option selection
themeOptionBtns.forEach((btn, idx) => {
btn.onclick = () => {
document.body.classList.remove(themes[currentTheme]);
currentTheme = idx;
document.body.classList.add(themes[currentTheme]);
themeOptionBtns.forEach(b => b.classList.remove('selected'));
btn.classList.add('selected');
themeOptions.style.display = 'none';
};
});
// Optional: Save todos and theme to localStorage for persistence
// ... existing code ...