Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I am trying to use local storage to save one of my arrays, pokemonchosen.here is the code:

var pokemonchosen = [];


How would I make it to where if refresh or browser close, the array pokemonchosen stays like it is? But I have a weirder problem than others, pokemonchosen doesn't have a value until someone buys a pokemon, in which case:

pokemonchosen.push("Charmander");


How would I make it where sets local storage on buy pokemon? This is one of many functions for pokemon:

function buymagikarp() {
  if(coins >= 100 ) {
    swal("Success", "You bought 1 magikarp!", "success");
    coins -= 100;
    pokemonchosen.push("Magikarp");
    document.getElementById("p1").innerHTML = "Coins: " + coins;   
    localStorage.setItem('coins', coins);
  } else {
    swal("Error", "Not enough balance", "error");
  }
}


Thank you :-)

What I have tried:

if(pokemonchosen != undefined) {
var JSONReadyUsers = JSON.stringify(pokemonchosen);
localStorage.setItem('pokemonchosen', JSONReadyUsers)
}
Posted
Updated 4-Sep-20 14:14pm
Comments
ZurdoDev 3-Aug-17 11:21am    
localStorage.setItem('pokemonchosen', JSONReadyUsers) should work, so what is the problem?

Your code in "What I have tried" will work, so you have that already! Here is how you can load the array again:
JavaScript
pokemonchosen = JSON.parse(localStorage.getItem('pokemonchosen'));

JSON.parse() - JavaScript | MDN[^]
 
Share this answer
 
v2
Comments
Member 13257242 3-Aug-17 11:34am    
Where would I put this? inside or outside of the if statement?
Thomas Daniels 3-Aug-17 11:43am    
This code (i.e. getItem) needs to run on your page load, to restore the chosen pokemons.

And for setItem, you have to run that every time that 'pokemonchosen' gets updated.
Member 13257242 3-Aug-17 11:37am    
And also, pokemonchosen starts out at undefined, so when they buy a pokemon everything in the if statement should be executed correctly?
Member 13257242 3-Aug-17 14:15pm    
It gives me the error:
Uncaught ReferenceError: JSONReadyUsers is not defined
at buymagikarp (script.js:35)
at HTMLInputElement.onclick (battle.html:131)

I'm sure this means that the Jquery is not working. Here is the tags that I put inside of the battle.html :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>&script type="text/javascript">function getName2 {
var JSONReadyUsers = JSON.stringify(pokemonchosen);
localStorage.setItem('pokemonchosen', JSONReadyUsers)
}</script>
Thomas Daniels 3-Aug-17 14:17pm    
Well, if you are using JSONReadyUsers outside of getName2, it won't work because the scope of JSONReadyUsers is only getName2.
You must first declare "coins" as a variable before you can use it.
example: var coins = localStorage.getItem ('coins');
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900