3

I've noticed that in Firefox an input element maintains its value when the page is reset but not in Safari. Is there a way to maintain the values a user would have typed in JavaScript (no PHP).

I've tried Googling this fairly extensively but every result seems to find this behavior undesirable.

Edit: Not sure what I am doing wrong, this is what I tried:

<input id="myInput" type="text">
<script>
    var myInput = document.getElementById("myInput");

    if (sessionStorage.getItem("autosave"))
        myInput.value = sessionStorage.getItem("autosave");

    myInput.addEventListener("change", function() {
        sessionStorage.setItem("autosave", myInput.value);
    });
</script>

Edit (again): Got it working, thank you:

<input id="myInput" type="text">
<script>
    var myInput = document.getElementById("myInput");

    window.onload = function() {
        if (sessionStorage.getItem("autosave"))
            myInput.value = sessionStorage.getItem("autosave");
    }

    myInput.addEventListener("keyup", function() {
        sessionStorage.setItem("autosave", myInput.value);
    });
</script>
2
  • You could save values to localstorage on input change, and clear on submit. Then, when loading the page, you check for localstorage values and populate the form. Commented Jul 16, 2014 at 5:36
  • This is browser feature , even in Firefox input elements will get reset if you do "hard-refresh" by clicking CTRL+F5. Commented Jul 16, 2014 at 5:37

5 Answers 5

5

I think sessionStorage would be ideal for this since localStorage and cookies would persist the values across sessions which is probably not what you would want.

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

2 Comments

Nice. I didn't know about that option. :-) It seems to be supported by all modern browsers caniuse.com/namevalue-storage
Yes, probably the best solution. (Cookies can be deleted / set to expire too by the way, but it requires to do more.)
2
<input id="persistent_text_field" value=""/>

In your JS assuming you're using jQuery

$(document).on('ready',function(){
    if(sessionStorage.getItem('last_entry')){
        $("#persistent_text_field").val(sessionStorage.getItem('last_entry'));
    }

    $("#persistent_text_field").on("keypress",function(){
        sessionStorage.setItem('last_entry',$(this).val());
    });
});

EDIT: Non jQuery Solution? Simple :)

<input id="persistent_text_field" value="" onkeypress="setStorage(this)"/>

In your JavaScript file call customReset(); in your document load event handler function.

function customReset(){
    if(sessionStorage.getItem('last_entry')){
        var element = document.getElementById("presistent_text_field");
        element.value = sessionStorage.getItem('last_entry');
    }
}

function setStorage(element){
    sessionStorage.setItem('last_entry',element.value);
}

1 Comment

Thank you but I would prefer a non jQuery solution
0

It is a browser behavior, but you can try to override it using cookies to remember entries, if you do not want to use PHP.

document.cookie = "field=value";

1 Comment

I am not sure how to make use of this, could you make an example? I updated my question to show some minimal HTML and JavaScript based on another answer
0

You need to store value in cookie or in local storage.

Remember text box value using jQuery cookies

1 Comment

Thank you but I would prefer a non jQuery solution
0

Firefox itself gives an answer to this, code and all in the link:

Session Storage

The example is given in Javascript

Comments

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.