본문 바로가기
카테고리 없음

[JavaScript] Momentum App - 4. ToDo-List

by 코딩삐약 2022. 5. 2.

//todo.js

const toDoForm = document.getElementById("todo-form");
const toDoInput = document.querySelector("#todo-form input");
const toDoList = document.getElementById("todo-list");

function paintToDo(newTodo){
    const li = document.createElement("li");
    const span = document.createElement("span");
    // span을 li 안에 넣기
    li.appendChild(span);
    // span에 텍스트 값 넣기 
    span.innerText = newTodo;
    // HTML에 li 추가
    toDoList.appendChild(li);
}

function handleToDoSubmit(event){
    event.preventDefault(); // window 기본동작(페이지 새로고침) 막기
    const newTodo = toDoInput.value; //input 값 저장
    toDoInput.value = "";
    paintToDo(newTodo); // paintTodo에 값 전달
}

toDoForm.addEventListener("submit", handleToDoSubmit);

// index.html

<!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/style.css">
        <title>Momentum App</title>
    </head>
    <body>
        <form class="hidden" id="login-form">
            <input 
            maxlength="15"
            required
            type="text" placeholder="What is your name?" />
            <input type="submit" value="Log In">
        </form>
        <h2 id="clock">00:00</h2>
        <h1 class="hidden" id="greeting"></h1>
        <form id="todo-form">
            <input type="text" placeholder="Write a To Do and Press Enter">
        </form>
        <ul id="todo-list">
        </ul>
        <div id="quote">
            <span></span>
            <span></span>
        </div>
        <script src="./js/clock.js"></script>
        <script src="./js/greetings.js"></script>
        <script src="./js/quotes.js"></script>
        <script src="./js/background.js"></script>
        <script src="./js/todo.js"></script>
    </body>
</html>