-1

i got some saved variables using localstorage, what i want to do is to put them into textfields automatically each time the page loads

<script language="javascript">

function save() {
    var value1 = document.getElementById("email").value;
    var value2 = document.getElementById("password").value;

    localStorage.setItem("eaddress", value1);
    localStorage.setItem("pwd", value2);

}

  </script>

i've got some textfields on the page namely email and password

ama
  • 79
  • 6

1 Answers1

0

If you want to do this with jQuery, this should accomplish what you're trying to do. Please note that I couldn't get the demo running in the snippet below because Stack Overflow is preventing form submissions.

$(function() {
  var
    $email = $('#email'),
    $password = $('#password'),
    emailValue = localStorage.getItem("eaddress"),
    passwordValue = localStorage.getItem("pwd");

  // SAVE VARIABLES TO LOCAL STORAGE
  $('form').on('submit', function() {
    localStorage.setItem("eaddress", $email.val());
    localStorage.setItem("pwd", $password.val());
  });
  
  // IF EMAIL AND PASSWORD ARE SAVED, PREPOPULATE THE FORM FIELDS
  if (emailValue != null) {
    $email.val(emailValue);
  }
  if (passwordValue != null) {
    $password.val(passwordValue);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="email" id="email" />
  <input type="password" id="password" />
  <button type="submit">Submit</button>
</form>
ReLeaf
  • 1,368
  • 1
  • 10
  • 17