I am trying to get new input values in a given textbox to save even after refreshing. Here is my attempt:
Event handler script:
//store value after changes
function store(){
var text = document.getElementById("qty").value;
localStorage.setItem("qty",text);
}
//local storage to keep values after refresh
function getValue(){
var storedText = localStorage.getItem("qty");
if(storedText != null){
document.getElementById("qty").value = storedText;
}
else
document.getElementById("qty").value = 0;
}
Event registration script:
document.getElementById("qty").onkeyup = store;
document.getElementById("qty").onload = getValue;
HTML:
<input type="text" id="qty" name="qty" class="small w-25 text-right" value="1">
My reasoning is that, once values are changed (onkeyup
), set the new value into localStorage
. Then if I refresh page, onload
should activate and use getValue()
to set the textbox value to the previously set localStorage
value.
However, when I refresh, it's always 1. I think I may be misunderstanding something regarding localStorage
. Any help is appreciated thanks.
*EDIT: got it to work thanks to comment below by changing getValue
to getValue()
However now I have this new problem:
I have the text input box on multiple pages. Now that I've got it to work on saving new input, it also changes the input value of all the other text boxes because they all have the same id. Would there be a way to make them save their own modified values instead of all the same without changing ids?