-
Notifications
You must be signed in to change notification settings - Fork 0
/
greeting.js
42 lines (33 loc) · 957 Bytes
/
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
const inputForm = document.querySelector(".js-inputForm");
const input = inputForm.querySelector("input");
const greet = document.querySelector(".js-greetings");
const LS_NAME = "name";
const CL_GREET = "greetings";
function greeting(name) {
greet.innerText = "Hello, " + name;
greet.classList.remove(CL_GREET);
inputForm.classList.add(CL_GREET);
}
function saveName(name) {
localStorage.setItem(LS_NAME, name);
}
function submitListener(event) {
event.preventDefault();
const name = input.value;
// save name
if(name) {
saveName(name);
} else {
alert("Please enter your name.");
}
greeting(name);
}
function init() {
inputForm.addEventListener("submit", submitListener);
// if data exists, say hello. if not, do nothing.
const savedName = localStorage.getItem(LS_NAME);
if(savedName) {
greeting(savedName);
}
}
init();