-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (53 loc) · 2.09 KB
/
script.js
File metadata and controls
60 lines (53 loc) · 2.09 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
document.addEventListener('DOMContentLoaded', () => {
const titleInput = document.getElementById('todo-title');
const detailsInput = document.getElementById('todo-details');
const saveButton = document.getElementById('save-button');
const todoList = document.getElementById('todo-list');
let todos = JSON.parse(localStorage.getItem('todos')) || [];
const saveTodos = () => {
localStorage.setItem('todos', JSON.stringify(todos));
};
const renderTodos = () => {
todoList.innerHTML = '';
todos.forEach((todo, index) => {
const li = document.createElement('li');
li.innerHTML = `
<div class="todo-item">
<span class="todo-title">${todo.title}</span>
<p class="todo-details-view" style="display: none;">${todo.details}</p>
</div>
<button class="delete-button" data-index="${index}">삭제</button>
`;
const titleElement = li.querySelector('.todo-title');
titleElement.addEventListener('click', () => {
const detailsView = li.querySelector('.todo-details-view');
if (detailsView) {
detailsView.style.display = detailsView.style.display === 'none' ? 'block' : 'none';
}
});
todoList.appendChild(li);
});
};
saveButton.addEventListener('click', () => {
const title = titleInput.value.trim();
const details = detailsInput.value.trim();
if (title) {
todos.push({ title, details });
saveTodos();
renderTodos();
titleInput.value = '';
detailsInput.value = '';
} else {
alert('제목을 입력해주세요.');
}
});
todoList.addEventListener('click', (e) => {
if (e.target.classList.contains('delete-button')) {
const index = e.target.getAttribute('data-index');
todos.splice(index, 1);
saveTodos();
renderTodos();
}
});
renderTodos();
});