상세 컨텐츠

본문 제목

vanilla JS #2

CODING/Javascript

by 피그말리온(PYGM) 2020. 6. 9. 00:14

본문

<!DOCTYPE html>

<html>

  <head>

    <title>something</title>

    <link rel="stylesheet" href="index.css" />

  </head>

  <body>

    <div class="js-clock">

      <h1 class="js-title">00:00</h1>

    </div>

    <form class="js-form form">

      <input type="text" placeholder="what is your name?" />

    </form>

    <h4 class="js-greetings greetings"></h4>

    <script src="clock.js"></script>

    <script src="gretting.js"></script>

  </body>

</html>

body {

  background-color: #ecf0f1;

}



h1 {

  color: #34495e;

}



.clicked {

  color: #7f8c8d;

}



.form,

.greetings {

  display: none;

}



.showing {

  display: block;

}

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();

    // she is not

  } else {

    //she is

    paintGreeting(currentUser);

  }

}



function init() {

  loadName();

}

init();
반응형

관련글 더보기

댓글 영역