๐Ÿ‘ฉ๐Ÿผโ€๐Ÿ’ป๋ฐ”๋‹๋ผ JS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ :#7.0~7.2

change upยท2023๋…„ 6์›” 14์ผ
0

- 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 ํ•˜๋ฉด ์นธ์„ ๋นˆํ™”๋ฉด์œผ๋กœ ๋น„์šฐ๋„๋ก ๋งŒ๋“ ๋‹ค.)
profile
์ƒˆ์‹น์ด

0๊ฐœ์˜ ๋Œ“๊ธ€