-2

I'm working on a web app with Vanilla JS, CSS and HTML. In my app, I have an input field where you should write your name. When you submit the value, it should be stored and shown on the front page/settings. After refreshing the page the value disappears. Why?

window.save_data = function () {
  if (typeof (Storage) !== "undefined") {
    let input = document.getElementById('inputName').value;
    localStorage.setItem('name', input);
    document.getElementById('inputName').value = localStorage.getItem('name');
    let storedValue = localStorage.getItem("name");
    document.querySelector("#user-name").innerHTML = storedValue;
    console.log(storedValue);
    return storedValue;
  } else {
    alert("Sorry! No Web Storage support..")
  }
}
<input type='text' name="name" id="inputName" />
      <button onclick="save_data()" type="button">Save</button>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Marcel
  • 29
  • 4

2 Answers2

0

To do this, please take a look and read about local storage ....

Azzabi Haythem
  • 2,318
  • 7
  • 26
  • 32
Chris Adams
  • 1,376
  • 1
  • 8
  • 15
0

Local Storgae is what you can use of for your scenario.

rajabraza
  • 333
  • 3
  • 11