Saving the User Name

김서하·2021년 4월 20일
0
const form = document.querySelector("#js-form"),
     input = form.querySelector("input"),
  greeting = document.querySelector("#js-greetings);
  
const USER_LS = "currentUser",
   SHOWING_CN = "showing";
   
function saveName(text) {
    localStorage.setItem(USER_LS, text);
}

function handleSubmit(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();
 
 
profile
개발자 지망생 서하입니당

0개의 댓글