<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Momentum App</title>
<link rel="stylesheet" href="CSS/style.css">
</head>
<body>
<form id="login-form" class="hidden">
<input
required
maxlength="10"
type="text"
placeholder="What is your name?" />
<input type="submit" value="Log In" />
</form>
<h2 id="clock">00:00</h2>
<h1 id="greeting" class="hidden"></h1>
<form id="todo-form">
<input type="text" placeholder="Write a To Do and Press Enter" required/>
</form>
<ul id="todo-list"></ul>
<div id="quotes">
<span></span><br/>
<span></span>
</div>
<script src="JS/greetings.js"></script>
<script src="JS/clock.js"></script>
<script src="JS/Quotes.js"></script>
<script src="JS/background.js"></script>
<script src="JS/todo.js"></script>
</body>
</html>
JS
const toDoForm = document.querySelector("#todo-form");
const toDoList = document.querySelector("#todo-list");
const toDoFormInput = toDoForm.querySelector("input");
const toDos = [];
function saveTodos () {
localStorage.setItem("todos", JSON.stringify(toDos));
}
function deleteToDo(e) {
const li = e.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);
}
function handleTodoSubmit (e) {
e.preventDefault();
const newTodo = toDoFormInput.value;
toDoFormInput.value = "";
toDos.push(newTodo);
paintToDo(newTodo);
saveTodos();
}
toDoForm.addEventListener("submit", handleTodoSubmit);
JSON.stringify([1,2,3,4]); //'[1,2,3,4]'
JSON.parse('[1,2,3,4]'); // {0:1, 1:2, 2:3, 3:4}
localStorage.getItem("todos");
// '["a","b","c","a","d"]'
// parse 함수를 이용하면 배열 형태로 변환되어
// 로컬스토리지에 넣어 키를 통해 객체의 값에 접근 할 수 있게된다 !
const TODOS_KEY = "todos";
const savedToDos = localStorage.getItem(TODOS_KEY);
if (savedToDos !== null) {
const parseToDos = JSON.parse(savedToDos);
parseToDos.forEach();
}
function sayHello() {
console.log("Hello!!");
}
parseToDos.forEach(sayHello);
// 3 * Hello!!
// 현재 parseToDos 에는 파싱된 3개의 아이템이 들어있다.
// 단순 for문으로 변환한다면?
for (let i = 0; i < parseToDos.length; i++) {
sayHello();
}
(드디어 이해했다 forEach!)
const TODOS_KEY = "todos";
function sayHello(e) {
console.log("this is turn of ", e);
}
const savedToDos = localStorage.getItem(TODOS_KEY);
if (savedToDos !== null) {
const parseToDos = JSON.parse(savedToDos);
parseToDos.forEach(sayHello);
};
// 위와 같이 코드가 작성 되어있다면?
콘솔:
if (savedToDos !== null) {
const parseToDos = JSON.parse(savedToDos);
console.log(parseToDos);
parseToDos.forEach((item) => {"This is turn of ", item});
}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Momentum App</title>
<link rel="stylesheet" href="CSS/style.css">
</head>
<body>
<form id="login-form" class="hidden">
<input
required
maxlength="10"
type="text"
placeholder="What is your name?" />
<input type="submit" value="Log In" />
</form>
<h2 id="clock">00:00</h2>
<h1 id="greeting" class="hidden"></h1>
<form id="todo-form">
<input type="text" placeholder="Write a To Do and Press Enter" required/>
</form>
<ul id="todo-list"></ul>
<div id="quotes">
<span></span><br/>
<span></span>
</div>
<script src="JS/greetings.js"></script>
<script src="JS/clock.js"></script>
<script src="JS/Quotes.js"></script>
<script src="JS/background.js"></script>
<script src="JS/todo.js"></script>
</body>
</html>
JS
const toDoForm = document.querySelector("#todo-form");
const toDoList = document.querySelector("#todo-list");
const toDoFormInput = toDoForm.querySelector("input");
const TODOS_KEY = "todos";
const toDos = [];
function saveTodos () {
localStorage.setItem(TODOS_KEY, JSON.stringify(toDos));
}
function deleteToDo(e) {
const li = e.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);
}
function handleTodoSubmit (e) {
e.preventDefault();
const newToDo = toDoFormInput.value;
toDoFormInput.value = "";
toDos.push(newToDo);
paintToDo(newToDo);
saveTodos();
}
function handleTodoRemove() {
console.log("Click!!")
}
toDoForm.addEventListener("submit", handleTodoSubmit);
const savedToDos = localStorage.getItem(TODOS_KEY);
if (savedToDos !== null) {
const parseToDos = JSON.parse(savedToDos);
console.log(parseToDos);
parseToDos.forEach((item) => {"This is turn of ", item});
}
다음 포스팅에서 !🙋🏻♂️