- ToDoList ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ.
+todo.js ์ฐ๊ฒฐ / todo form๋ง๋ค๊ธฐ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel = "stylesheet" href="css/app.css">
<title>Momentum</title>
</head>
<body>
<h2 id ="clock">00:00:00</h2>
<form class="hidden" id="login-form">
<input type="text" />
<button id="button">Log In</button>
</form>
<h1 id="greeting" class="hidden"></h1>
<form id = "todo-form">
<input type = "text" placeholder="write a To Do and Press Enter" required/>
</form>
<ol id = "todo-list"></ol>
<div id="quote">
<span></span>
<span></span>
</div>
<script src="js/clock.js"></script>
<script src="js/greeting.js"></script>
<script src="js/quotes.js"></script>
<script src="js/background.js"></script>
<script src="js/todo.js"></script>
</body>
</html>
/*+toDoForm์ ์
๋ ฅ๋ ๊ฐ์ด ์๋ฒ๋ก ๋ณด๋ด์ง๋๋ก submitํ๊ณ ๊ทธ๋ ๊ฒ ์์ ๋ชฉ๋ก๋ค์ด ๋ณด์ฌ์ง๋๋ก ol-li๋ก
์ซ์๋ก ์์๊ฐ ํํ๋๋๋ก ํ ํ , ํด๋น ๋ชฉ๋ก์ ์ญ์ ํ ์ ์๋ delete button์ ์ถ๊ฐํจ.*/
const toDoForm = document.getElementById("todo-form");
const toDoInput = toDoForm.querySelector("input");
const toDolist = document.getElementById("todo-list");
//event.target์ ์ฌ์ฉํ๋ฉด ํ์ฌ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ์์์ ์์ฑ๋ค์ ์ป์ ์ ์๋ค.
//Dom ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๋ณ๊ฒฝ์ ์ฃผ๊ณ ์ถ๋ค๋ฉด Dom๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
//button ์์ฑ์ ๋ถ๋ชจ์์๋ก li๋ฅผ ๊ฐ๋ฅดํจ๋ค.
function deleteToDo(event) {
const li = event.target.parentElement;
li.remove();
}
function paintToDo(newTodo) {
const li = document.createElement("li");
const span = document.createElement("span");
span.innerText = newTodo;
const button = document.createElement("button");
button.innerText = "๐
๐ป";
button.addEventListener("click",deleteToDo)
li.appendChild(span);
li.appendChild(button);
toDolist.appendChild(li)
}
/* ์ญ์ ๊ธฐ๋ฅ๋ ๊ฐ๋ฅํ๋๋ก:span์ ๋ง๋ค์ด์ li ๋ด๋ถ์ ์์์ผ๋ก ๋ฃ์ ๊ทธ ๋ค์์ new ToDo์ ์
๋ ฅ๋ฐ์ ๊ฐ์ span๋ด๋ถ์ ๋ฃ์ */
function handleToDoSubmit(event) {
event.preventDefault();
const newTodo = toDoInput.value;
toDoInput.value = "";
paintToDo(newTodo)
}
toDoForm.addEventListener("submit",handleToDoSubmit)
//toDoInput.value = "";(enter ํ๋ฉด ์นธ์ ๋นํ๋ฉด์ผ๋ก ๋น์ฐ๋๋ก ๋ง๋ ๋ค.)