-
Notifications
You must be signed in to change notification settings - Fork 0
/
greeting.js
59 lines (44 loc) · 1.57 KB
/
greeting.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
54
55
/*
const loginForm = document.getElementById("login-form")
const loginInput = loginForm.querySelector("input"); // we don't need search from document
const loginButton = loginForm.querySelector("button");
loginButton.addEventListener("click", handleButtonClick);
function handleLinkClick(event) {
event.preventDefault();
alert("clicked!");
}
link.addEventListener("click", handleLinkClick);
<a href="https://nomadcoder.co">Go to courses</a>
*/
const loginForm = document.querySelector("#login-form")
const loginInput = document.querySelector("#login-form input");
//const loginButton = document.querySelector("#login-form button");
const greeting = document.querySelector("#greeting");
const HIDDEN_CLASSNAME = "hidden";
const USERNAME_KEY = "username";
function onLoginSubmit(evnet) {
event.preventDefault();
const username = loginInput.value;
loginForm.classList.add(HIDDEN_CLASSNAME);
console.log(username);
localStorage.setItem(USERNAME_KEY, username);
paintGreetings(username);
/*
if (username === "") {
alert("Please write your name!");
} else if(username.length > 15) {
alert("your name is too long");
}
*/
}
function paintGreetings(username) {
greeting.innerText = "Hello, " + username; // `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);
}