Need help with storage | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help with storage

I have never used local storage. I want to keep last changes after closing browser, it should not reset after re-open website. https://code.sololearn.com/WoWrkm8BF4x8/?ref=app

23rd Feb 2022, 5:43 AM
Salman B
Salman B - avatar
9 Answers
+ 1
Salman B it is simple just add value with key localStorage.setItem(key, value) key should be unique because there might be many different keys Value - whatever you want to get next time when you open browser. Don't destroy your localStorage key otherwise next time you won't get that value. To get value from localStorage use getItem function like: var value = localStorage.getItem(key); //Here key should be same as you have stored in localStorage.
23rd Feb 2022, 7:48 AM
A͢J
A͢J - avatar
+ 1
Salman B If you want to write when page load then write inside window.onload function
23rd Feb 2022, 1:52 PM
A͢J
A͢J - avatar
0
Learn localStorage From here https://www.sololearn.com/learning/2197/
23rd Feb 2022, 6:35 AM
A͢J
A͢J - avatar
0
I have checked your link. But i am not getting how to use it in my code. Can you just help me out?
23rd Feb 2022, 6:52 AM
Salman B
Salman B - avatar
0
I tried this. Do it need to add function? function myfunc(event) { var fajr = document.getElementById("myP").value; var zohar = document.getElementById("myZ").value; var asar = document.getElementById("myA").value; var maghrib = document.getElementById("myM").value; var isha = document.getElementById("myI").value; localStorage.setItem('ls_myP', myP); localStorage.setItem('ls_myZ', myZ); localStorage.setItem('ls_myA', myA); localStorage.setItem('ls_myM', myM); localStorage.setItem('ls_myI', myI); };
23rd Feb 2022, 8:26 AM
Salman B
Salman B - avatar
0
Salman B No need of function but if you are writing inside function then your function should be call once otherwise data will not be store in localStorage. Once data stores in localStorage then you can access anytime.
23rd Feb 2022, 12:13 PM
A͢J
A͢J - avatar
0
I don't want function then how to write this?
23rd Feb 2022, 12:42 PM
Salman B
Salman B - avatar
0
How to write this without Functions.
23rd Feb 2022, 1:01 PM
Salman B
Salman B - avatar
0
Can you please guide me. This is what i wrote. Code is long. I cannot post it here function myfunc(event) { var fajr = document.getElementById("myP").value; var zohar = document.getElementById("myZ").value; var asar = document.getElementById("myA").value; var maghrib = document.getElementById("myM").value; var isha = document.getElementById("myI").value; localStorage.setItem('ls_myP', myP); localStorage.setItem('ls_myZ', myZ); localStorage.setItem('ls_myA', myA); localStorage.setItem('ls_myM', myM); localStorage.setItem('ls_myI', myI); };
23rd Feb 2022, 1:56 PM
Salman B
Salman B - avatar