Saving the User Name
์ด๋ฆ ์
๋ ฅ๋ฐ๊ณ ์ ์ฅํ๊ธฐ
html
<section class="center">
<div class="js-clock clock">
<h3 class="clock__text">00:00</h3>
</div>
<div class="js-name name"></div>
<div>
<form class="js-to-do to-do">
<input class="js-add-to-do to-do__add-to-do" placeholder="Write a To Do here" type="text" />
</form>
<ul class="js-list list"></ul>
</div>
</section>
<div class="js-location location">
<span class="location__text"></span>
</div>
<div class="js-weather weather">
<span class="weather__text"></span>
</div>
<script src="js/clock.js"></script>
<script src="js/greeting.js"></script>
<script src="js/todo.js"></script>
javascript
const nameContainer = document.querySelector(".js-name"); // ์ด๋ฆ์ด ๋์ฌ ๋ถ๋ถ
function paintName(name) {
nameContainer.innerHTML = ""; // .js-name html์ ์ธ๊ฒ์ ์ ์ธ
const title = document.createElement("span"); // .js-name ํ์์ span ์์ฑ
title.className = "name__text"; //js-name ํ์์ span์ name__text ํด๋์ค๋ช
์ถ๊ฐ
title.innerHTML = `Hello ${name}`; // span ์์ Hello ์
๋ ฅ๋ ์ด๋ฆ์ด ์
๋ ฅ๋๊ฒํ๋ค.
nameContainer.appendChild(title); // nameContainer์ ์์ ์ถ๊ฐํ๋ค.
}
function handleSubmit(event) {
event.preventDefault();
const form = event.target;
const input = form.querySelector("input"); // form -> input
const value = input.value; // input์ value๊ฐ ์ ์ฅ
localStorage.setItem("username", value);
paintName(value);// ์
๋ ฅํ๊ฐ์ value ๊ฐ์ผ๋ก ์ ์ฅ
}
function paintInput() {
const input = document.createElement("input");
input.placeholder = "Type your name here";
input.type = "text";
input.className = "name__input";
const form = document.createElement("form");
form.addEventListener("submit", handleSubmit);
form.appendChild(input);
nameContainer.appendChild(form);
}
function loadName() {
const name = localStorage.getItem("username");
if (name === null) {
paintInput();
} else {
paintName(name);// ์ด๋ฆ์ ์
๋ ฅ๋ฐ์๋ ์ถ๋ ฅ
}
}
function init() {
loadName();
}
init();
Last updated
Was this helpful?