-
Notifications
You must be signed in to change notification settings - Fork 0
/
gretting.js
45 lines (40 loc) · 1.01 KB
/
gretting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const form = document.querySelector(".js-form"),
input = form.querySelector("input"),
greeting = document.querySelector(".js-grettings");
const USER_LS = "currentUser",
SHOWING_CN = "showing";
function saveName(text) {
//사용자를 기억
localStorage.setItem(USER_LS, text);
}
function handleSubmit() {
//form형식을 제출할때 event
event.preventDefault();
const currentValue = input.value;
paintGreeting(currentValue);
saveName(currentValue);
}
function askForName() {
//사용자입력 받기
form.classList.add(SHOWING_CN);
form.addEventListener("submit", handleSubmit);
}
function paintGreeting(text) {
//화면에 표시
form.classList.remove(SHOWING_CN);
greeting.classList.add(SHOWING_CN);
greeting.innerText = `Hello ${text}`;
}
function loadName() {
//사용자가 있는지 없는지
const currentUser = localStorage.getItem(USER_LS);
if (currentUser == null) {
askForName();
} else {
paintGreeting(currentUser);
}
}
function init() {
loadName();
}
init();