๐ŸŽจ to-do-list

kirin.logยท2021๋…„ 3์›” 9์ผ
1
post-custom-banner

๐Ÿช logic

  • input์— ๊ธ€์„ ์“ฐ๊ณ  button์„ ๋ˆ„๋ฅธ๋‹ค
  • input.value๊ฐ€ <ul>์•ˆ์˜ <li>๋กœ ์ƒ์„ฑ๋œ๋‹ค
  • input์ฐฝ์€ defalt๊ฐ€ ๋œ๋‹ค
  • โŒ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ๊ฐ€ ์‚ญ์ œ๋œ๋‹ค
๐ŸŽƒ 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" />
        <title>to-do-list</title>
        <link href="index.css" rel="stylesheet" />
        <script defer src="index.js"></script>
    </head>
    <body>
        <div class="container">
            <h1>To-Do-List</h1>
            <div class="inputContainer">
                <input type="text" placeholder="Write your list..." />
                <button>Write</button>
            </div>
            <ul></ul>
        </div>
    </body>
</html>
๐ŸŽƒ css

* {
    box-sizing: border-box;
}

body {
    background: linear-gradient(to right, skyblue, orange);
}

.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: auto;
    width: 350px;
    height: 70vh;
    border: 2px solid coral;
}

.inputContainer {
    display: flex;
    justify-content: space-between;
    width: 100%;
    padding: 10px;
}

input {
    width: 90%;
}

ul {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 100%;
    padding: 10px;
    margin: 0;
    list-style: none;
}

li {
    display: flex;
    justify-content: space-between;
    padding: 5px 0;
}

.error {
    border: 2px solid red;
}
๐ŸŽƒ js

const input = document.querySelector("input");
const btn = document.querySelector("button");
const ul = document.querySelector("ul");

// ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ž…๋ ฅ-์ถœ๋ ฅ ์ด๋ฒคํŠธ ๋ฐœ์ƒ
btn.addEventListener("click", (e) => {
    // input๊ฐ’์€ ๋ณ€์ˆ˜ํ™” ํ•ด์ค€๋‹ค
    let txt = input.value;

    let li = document.createElement("li");
    // input๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ error์ฐฝ ๋œจ๋„๋ก
    if (txt < 1) {
        input.classList.add("error");
    } 
    // input๊ฐ’ ์žˆ์„ ๊ฒฝ์šฐ, ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ ๋ฐ input์ฐฝ์— ๊ฐ’ ์‚ญ์ œ
    else {
        ul.appendChild(li);
        li.innerHTML = `${txt}  <button class="delBtn">โŒ</button>`;
        input.value = "";
        input.classList.remove("error");
    }

    // delBtn์€ ํ•ด๋‹น ๋ธ”๋Ÿญ(btn.addEventListener) ์•ˆ์—์„œ ์ƒ์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ™์€ ์˜์—ญ ๋‚ด์—์„œ ์ง€์ • 
    delBtn = document.querySelectorAll(".delBtn");

    // delBtn์€ ๋ณต์ˆ˜(=๋ฐฐ์—ด)์ด๋‹ค. (์—ฌ๋Ÿฌ๊ฐœ ์ƒ์„ฑ ๊ฐ€๋Šฅ)
    delBtn[delBtn.length - 1].addEventListener("click", (e) => {

        // ul์—์„œ li๋ฅผ ์ง€์›Œ์ค€๋‹ค(e.target.parentElement = li)
        ul.removeChild(e.target.parentElement);
    });
});
profile
boma91@gmail.com
post-custom-banner

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

comment-user-thumbnail
2021๋…„ 3์›” 9์ผ

์ƒˆ๋ด„๋‹˜! ์‚ฌ์šฉํ•˜์‹  ์ด๋ชจ์ง€๋“ค์ด ๋„ˆ๋ฌด ๊ท€์—ฝ๋„ค์š”ใ…‹ใ…‹ใ…‹ ์ „ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‹ค ๊นŒ๋จน์—ˆ์–ด์—ฌ...ใ…Žใ…Ž

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ