How can you make the browser remember that the user entered into a form that has not yet been submitted, and make the page refresh not affect the entered data?
I have a form in which the user enters a number. By default, the form has 0 by default. I store data in localStorage, so the browser can remember the data. However, when the page refreshes, the data entered by the user disappears, and the default is 0. (localStorage data exists for this)
I tried using jQuery
$(".formClassName").val(localStorage.getItem(key));
but that will not work. Can anyone give me some advice on this? Thanks in advance.
Edited: My form is as follows:
<form> Enter a number <input type="text" value="0" class"dataEntered" name="****"> <input type="button" class="savedata" value="Save Value" name="****"> </form>
And I am adding data to localStorage as below:
//JavaScript <script> //Using on because the website retrieves the above form dynamically $(document).on("click", ".saveData", function(e){ //retrieve the number entered in the form var userNum = $(this).siblings(".dataEntered").val(); //retrieve the value in name attribute var thisFormName = $(this).attr("name"); //store the data localStorage.setItem(thisFormName, userNum); //Now that the save button has been pressed (not submitted to the //server yet), and the data is stored in localStorage, I want to //the page to show the number in userNum even after you refresh the page //but this does not work. $(".dataEntered").val(localStorage.setItem(thisFormName)); }); </script>
source share