๐ช 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) => {
let txt = input.value;
let li = document.createElement("li");
if (txt < 1) {
input.classList.add("error");
}
else {
ul.appendChild(li);
li.innerHTML = `${txt} <button class="delBtn">โ</button>`;
input.value = "";
input.classList.remove("error");
}
delBtn = document.querySelectorAll(".delBtn");
delBtn[delBtn.length - 1].addEventListener("click", (e) => {
ul.removeChild(e.target.parentElement);
});
});
์๋ด๋! ์ฌ์ฉํ์ ์ด๋ชจ์ง๋ค์ด ๋๋ฌด ๊ท์ฝ๋ค์ใ ใ ใ ์ ์๋ฐ์คํฌ๋ฆฝํธ ๋ค ๊น๋จน์์ด์ฌ...ใ ใ