vasilis 123
vasilis 123

Reputation: 665

localstorage not returning text on reload

This is my first time using localstorage in an html page where have an input element where I can insert text . When I reload my page I wnat the text to stay there but it doesn't .

//function to store name on localstorage
function storeName(){
  var name = document.getElementById("name");
  localStorage.setItem("user" , name.value);
  name.value= localStorage.getItem("user");
}
<input type="text" onchange="storeName()" id="name" placeholder="Enter name"/>

Upvotes: 0

Views: 48

Answers (1)

Derek Wang
Derek Wang

Reputation: 10194

The changes are set correctly on localStorage. When reloading, you can not see the text because you have not loaded the value.

Load the localStorage value on page load.

function storeName(){
  var name = document.getElementById("name");
  localStorage.setItem("user" , name.value);
}

document.getElementById("name").value= localStorage.getItem("user");
<input type="text" onchange="storeName()" id="name" placeholder="Enter name"/>

Upvotes: 2

Related Questions