-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
30 lines (24 loc) · 877 Bytes
/
app.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
const loginInput = document.querySelector("#login-form input");
const loginForm = document.querySelector("#login-form");
const greeting = document.querySelector("#greeting");
const HIDDEN_CLASSNAME = "hidden";
const USERNAME_KEY = "username";
function onLoginSubmit(event) {
event.preventDefault();
const username = loginInput.value;
localStorage.setItem(USERNAME_KEY, username);
loginForm.classList.add(HIDDEN_CLASSNAME);
paintGreetings(username);
}
loginForm.addEventListener("submit", onLoginSubmit);
function paintGreetings(username){
greeting.innerText = `Hello ${username}`;
greeting.classList.remove(HIDDEN_CLASSNAME);
}
const savedUsername = localStorage.getItem(USERNAME_KEY);
if(savedUsername == null){
loginForm.classList.remove(HIDDEN_CLASSNAME);
loginForm.addEventListener("submit", onLoginSubmit);
}else{
paintGreetings(savedUsername);
}