-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp3.js
53 lines (41 loc) · 1.41 KB
/
app3.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
46
47
48
49
50
51
52
53
const loginForm = document.querySelector(".login-form");
const loginInput = loginForm.querySelector("input");
const loginButton = loginForm.querySelector("button");
/* version1
function handleButtonClick() {
const username = loginInput.value;
if(username === ""){
alert("pleas write your name");
}else if(username.length > 15){
alert("your name is too long")
}
else{
console.log("click!!!!");
console.log("hello", loginInput.value);
}
}
loginButton.addEventListener("click", handleButtonClick);
*/
const HIDDEN_CLASSNAME ="hidden";
const USER_NAME_KEY = "user name";
const display = document.querySelector("#greeting");
function onLoginSubmit(tomato){
tomato.preventDefault();
const username = loginInput.value;
loginForm.classList.add(HIDDEN_CLASSNAME);
paintGreetings(username);
localStorage.setItem(USER_NAME_KEY, username);
}
loginForm.addEventListener("submit",onLoginSubmit )
const saveUsername = localStorage.getItem(USER_NAME_KEY);
function paintGreetings(username){
display.innerText = `Hello ${username}`;
display.classList.remove(HIDDEN_CLASSNAME);
}
if(saveUsername === null){
loginForm.classList.remove(HIDDEN_CLASSNAME);
loginForm.addEventListener("submit", onLoginSubmit);
}else{
paintGreetings(saveUsername);
loginForm.classList.add(HIDDEN_CLASSNAME)
}