'Shelf life of numbers in inputs
I have friends of the multi-page application. On one page there is a calculator mode in which three actually have a text box. And in the first two, you enter values, and then in the third, you enter a number. Now if I enter numbers and move from the page, the numbers will be deleted. But I do not want this to happen and I want the numbers to remain in the text boxes
Solution 1:[1]
You can use
let names=['input1','input2','input3'];
function parseCookie(text) {
if(text.length) {//if the length of the text is not 0
let values={};
for(let i=0;i<names.length;i++) {
let namePos=text.indexOf(names[i]+'=')+names[i].length+1;
values[names[i]]=text.slice(namePos,text.indexOf(';',namePos))
}
return values} else {return false}
}
let inputs=document.querySelectorAll('input'); //get the input areas
let cookies=parseCookie(document.cookie)
if(cookies) {
for(let i=0;i<names.length;i++) {
inputs[i].value=cookies[names[i]];
inputs[i].addEventListener('input',function() {document.cookie=names[i]+"="+document.getElementById(names[i]).value}++';expires=Thu, 18 Dec 3000 12:00:00 UTC')
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DharmanBot |
