์๊ตฌ์ฌํญ
- HTML ๊ตฌ์กฐ:
- ํ ์ผ ์
๋ ฅ ํ๋์ ๋ฒํผ์ ํฌํจํ๋ <form id="todo-form">์ ๋ง๋ญ๋๋ค.
- <input type="text" id="todo-input"> (ํ ์ผ ์ ๋ ฅ)
- <button type="submit">์ถ๊ฐ</button>
- ํ ์ผ ๋ชฉ๋ก์ด ํ์๋ ๋น <ul> ํ๊ทธ (id="todo-list")๋ฅผ ๋ง๋ญ๋๋ค.
- ํ ์ผ ์
๋ ฅ ํ๋์ ๋ฒํผ์ ํฌํจํ๋ <form id="todo-form">์ ๋ง๋ญ๋๋ค.
- CSS ์คํ์ผ (์ ํ ์ฌํญ):
- ์ถ๊ฐ๋ <li> ํญ๋ชฉ์ ๋ง์ฐ์ค ์ค๋ฒ ์ ๋ฐฐ๊ฒฝ์์ด ๋ฐ๋๋ ๋ฑ ๊ฐ๋จํ ์คํ์ผ์ ์ ์ฉํ์ฌ ์๊ฐ์ ์ผ๋ก ๊ตฌ๋ถํฉ๋๋ค.
- JavaScript ๊ธฐ๋ฅ (๊ฐ์ฅ ์ค์):
- ํ ์ผ ์ถ๊ฐ ๊ธฐ๋ฅ:
- ํผ (#todo-form)์ submit ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ถ๊ฐํฉ๋๋ค.
- ์ด๋ฒคํธ ๋ฐ์ ์ ํ์ด์ง ์๋ก๊ณ ์นจ์ ๋ง๊ณ (ํํธ: event.preventDefault()) ์ ๋ ฅ๋ ํ ์คํธ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
- ์๋ก์ด <li> ์์๋ฅผ ์์ฑํ๊ณ , ๊ทธ ์์ ์ ๋ ฅ๋ ํ ์คํธ์ ์ญ์ ๋ฒํผ (<button class="delete-btn">์ญ์ </button>)์ ํฌํจ์ํต๋๋ค.
- ์๋ก์ด <li> ์์๋ฅผ #todo-list์ ์ถ๊ฐํฉ๋๋ค.
- ์ ๋ ฅ ํ๋ (#todo-input)๋ฅผ ๋น์๋๋ค.
- ํ ์ผ ์ญ์ ๊ธฐ๋ฅ (์ด๋ฒคํธ ์์ ์ฌ์ฉ):
- ๋ชฉ๋ก ์ปจํ ์ด๋ (#todo-list)์ ํด๋ฆญ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ํ๋๋ง ์ถ๊ฐํฉ๋๋ค. (์๋ก ์์ฑ๋๋ ๋ชจ๋ ์ญ์ ๋ฒํผ์ ๊ฐ๋ณ ๋ฆฌ์ค๋๋ฅผ ์ถ๊ฐํ์ง ์์)
- ํด๋ฆญ๋ ์์๊ฐ .delete-btn ํด๋์ค๋ฅผ ๊ฐ์ง ๋ฒํผ์ธ์ง ํ์ธํฉ๋๋ค.
- ๋ฒํผ์ด ๋ง๋ค๋ฉด, ํด๋น ๋ฒํผ์ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ถ๋ชจ <li> ์์๋ฅผ DOM์์ ์ ๊ฑฐํฉ๋๋ค.
- ํ ์ผ ์ถ๊ฐ ๊ธฐ๋ฅ:
๋๋ณด๊ธฐ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0;
}
.todo {
border: 4px solid #000;
}
.todo__controls {
display: flex;
align-items: center;
column-gap: 5px;
padding: 15px;
border-bottom: 1px solid #333;
}
.todo__controls .todo__input {
height: 25px;
border: 1px solid #000;
border-radius: 0;
}
.todo__controls .todo__add-btn {
height: 25px;
background-color: #333;
cursor: pointer;
color: #fff;
}
.todo__list {
margin: 0;
list-style-type: circle;
color: #777
}
.todo__item {
padding: 10px 0;
}
.todo__item:hover {
color: #000;
text-decoration: line-through;
}
.todo__item .todo__del-btn {
margin-left: 5px;
background-color: tomato;
border: 1px solid tomato;
color: #fff;
cursor: pointer;
}
</style>
</head>
<body>
<form action="" id="todo-form" class="todo">
<div class="todo__controls">
<label class="todo__label">ํ ์ผ ์
๋ ฅ :
<input type="text" name="" id="todo-input" class="todo__input">
</label>
<button type="submit" class="todo__add-btn">์ถ๊ฐ</button>
</div>
<ul id="todo-list" class="todo__list"></ul>
</form>
<script>
document.addEventListener("DOMContentLoaded", () => {
const todoForm = document.getElementById('todo-form');
const todoInput = document.getElementById('todo-input');
const todoList = document.getElementById('todo-list');
todoForm.addEventListener('submit', (e) => {
e.preventDefault();
const taskText = todoInput.value.trim();
if (taskText === '') {
alert('ํ ์ผ์ ์
๋ ฅํด์ฃผ์ธ์!');
return;
}
const newItem = `
<li class="todo__item">
${taskText}
<button type="button" class="todo__del-btn">์ญ์ </button>
</li>
`;
todoList.insertAdjacentHTML('beforeend', newItem);
todoInput.value = '';
})
todoList.addEventListener('click', (e) => {
if (e.target.classList.contains('todo__del-btn')) {
const listItemToDelete = e.target.closest('li');
listItemToDelete.remove();
}
});
})
</script>
</body>
</html>
