I have this code for saving data from 2 inputs. Everything is all right when the page refreshes twice (the data is saved) but on third one the date disappear. I know this is because of the localStorage.removeItem
, but I need the data to disappear after the tab is gone.
I use the same method for other stuff and it's working perfect. but in this case it's how I said , working for just 2 refreshes .
<input type="date" id="exdated" name=""
onfocus="localStorage.setItem('myTestApp','exdated')"
onkeyup="saveValue(this);">
<br><br>
<input type="date" id="datata" name=""
onfocus="localStorage.setItem('myTestApp','datata')"
onkeyup="saveValue(this);">
<br><br>
let today = new Date().toISOString().slice(0, 10)
document.getElementById("datata").value = getSavedValue("datata");
document.getElementById("exdated").value = getSavedValue("exdated");
function saveValue(e) {
var id = e.id;
var val = e.value;
localStorage.setItem(id, val);
}
function getSavedValue (v) {
if (!localStorage.getItem(v)) {
return today;
}
var itemcheta = localStorage.getItem(v);
localStorage.removeItem(v);
return itemcheta;
}
– stoperan Sep 01 '21 at 12:15