2

I want to save data in the input when the page reloading and I don't know why my code doesn't work. This is my html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
        <label>nom:</label> <input type="text" value=""/></br>
        <label>prenom:</label><input type="text" value=""/>
        <script type="text/javascript">
        window.onbeforeunload = function(){
        localStorage.setItem(nom, document.getElementsByTagName('input')[0].value);
        localStorage.setItem(prenom, document.getElementsByTagName('input')[1].value);
            }
    document.addEventListener("DOMContentLoaded",function(){
            var nom = localStorage.getItem(nom);
            var prnom = localStorage.getItem(prenom);
            if(nom!==null&&prenom!==null) {                 
            document.getElementsByTagName('input')[0].value = nom;
            document.getElementsByTagName('input')[1].value = prenom;
            }
        });

         </script>
</body>
</html>
1
  • Your code was setting the localStorage values when the page was loaded which means it set it to be no text so the code won't save. Use the code from the snippet. Commented May 2, 2017 at 0:39

1 Answer 1

1

Make sure to use quotes for the variable name:

localStorage.setItem('nom', document.getElementsByTagName('input')[0].value);

Or you can use this which is simpler:

localStorage.nom = document.getElementsByTagName('input')[0].value;

Your code was setting the localStorage values when the page was loaded which means it set it to be no text so the code won't save.

Use the below code:

window.onbeforeunload = function() {
  var nom = localStorage.nom;
  var prnom = localStorage.prenom;
  if (nom !== null && prenom !== null) {
    document.getElementsByTagName('input')[0].value = nom;
    document.getElementsByTagName('input')[1].value = prenom;
  }
};
<label>nom:</label> <input type="text" onchange="localStorage.nom = document.getElementsByTagName('input')[0].value" /></br>
<label>prenom:</label><input type="text" onchange=" localStorage.prenom = document.getElementsByTagName('input')[1].value" />

IMPORTANT: use the code on your computer because localStorage doesn't work on stack overflow snippets

Sign up to request clarification or add additional context in comments.

5 Comments

i try it now and this solution doesn't solve the problem :/
Your code was setting the localStorage values when the page was loaded which means it set it to be no text so the code won't save. Use the code from the snippet.
thanx i try now and this fix my problem i set a default value in the input and reload and that s work good localStorage.nom = document.getElementsByTagName('input')[0].value;
Put localStorage.nom = localStorage.nom || "" in the onbeforeunload function to set a default value if it is undefined.
ok that s good , i went to ask you if you have a solution using angularjs ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.