Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello! I am trying to display a variable. People click on an image to pick a Pokemon (Charmander, Bulbasaur, or Squirtle), and it will take them to a page called battle.html and they will be able to pick more Pokemon with the coins they have. The code I am using for saving the pokemon chosen is

function move() {
  if(localStorage.getItem('pokemonchosen') === null) {
        localStorage.setItem('pokemonchosen', pokemon);
    } else {
        pokemonchosen = localStorage.getItem('pokemonchosen');
    }
    document.getElementById("p1").innerHTML =
        "Pokemon chosen: " + pokemonchosen;
}
}


Here are the images code:

<a href="battle.html" onclick="charmander();move();"><img draggable="false" src="images/charmander.gif"></a>
	<a href="battle.html" onclick="squirtle();move();"><img draggable="false" src="images/squirtle.gif"></a>
	<a href="battle.html" onclick="bulbasaur();move();"><img draggable="false" src="images/bulbasaur.gif"></a>


Here are all the variables:
var pokemonchosen;
var coins = 1000;
var pokemonagainstTutorial = "Magikarp";
var random = pokemonagainst[Math.floor(Math.random()*pokemonagainst.length)];
var username = document.getElementById('enterusername').innerHTML = username;
//HP For Pokemon - Start
var hpmagikarp = 100;
var hpcharmander = 100;
var hpbulbasaur = 100;
var hpsquirtle = 100;
//HP For Pokemon - End


I don't know if this matters: but the variables are on the top and I am not using a script tag for all the code it's external script. But yes I am linking it to it on battle.html :


UPDATE (from comments):
I edited the code a bit:
JavaScript
var pokemonchosen;
var coins = 1000;
var pokemonagainstTutorial = "Magikarp";
//HP For Pokemon - Start
var hpmagikarp = 100;
var hpcharmander = 100;
var hpbulbasaur = 100;
var hpsquirtle = 100;
//HP For Pokemon - End

function move() {
if(localStorage.getItem('pokemonchosen') === null) {
localStorage.setItem('pokemonchosen', pokemonchosen);
} else {
pokemonchosen = localStorage.getItem('pokemonchosen');
}
document.getElementById("p1").innerHTML =
"Pokemon chosen: " + pokemonchosen;
}

It still doesn't work but I'm closer: now it shows: Pokemon chosen: undefined

What I have tried:

I have tried adding the script all into the html with a script tag.
Posted
Updated 31-Jul-17 7:07am
v2
Comments
Member 13257242 31-Jul-17 11:54am    
I edited the code a bit:
<added to question>
Thomas Daniels 31-Jul-17 12:25pm    
When you call setItem, is 'pokemon' defined?
Member 13257242 31-Jul-17 12:35pm    
@ProgramFox , No, but I changed the code:
var pokemonchosen;
var coins = 1000;
var pokemonagainstTutorial = "Magikarp";
//HP For Pokemon - Start
var hpmagikarp = 100;
var hpcharmander = 100;
var hpbulbasaur = 100;
var hpsquirtle = 100;
//HP For Pokemon - End

function move() {
if(localStorage.getItem('pokemonchosen') === null) {
localStorage.setItem('pokemonchosen', pokemonchosen);
} else {
pokemonchosen = localStorage.getItem('pokemonchosen');
}
document.getElementById("p1").innerHTML =
"Pokemon chosen: " + pokemonchosen;
}

1 solution

From your updated code, these two lines are important:
JavaScript
var pokemonchosen;
JavaScript
localStorage.setItem('pokemonchosen', pokemonchosen);

pokemonchosen is undefined after that first line, but never gets a value. So the second highlighted line stores undefined into localStorage. And that's not very useful.

Somewhere in your code, you'll have to give pokemonchosen an actual value - before storing it in localStorage.
 
Share this answer
 
Comments
Member 13257242 31-Jul-17 13:17pm    
Thanks for the answer! Although I understand what you're saying, I don't know how to apply it to my code? For example, should I use prompts, buttons, or whatever? I am using buttons but when they click on the button to choose a pokemon, it just says Pokemon Chosen: null, I really don't get it. I'll likely take screenshots and send them to you so you can review them and see what's wrong.
Thomas Daniels 31-Jul-17 13:36pm    
Hmm.. "onclick" should get called before the link goes to the URL as set in the "href" attribute, so maybe the methods to set the pokemon are broken? That could explain it, but I can't see those.
Member 13257242 31-Jul-17 13:23pm    
Here are pics: http://imgur.com/a/GsEJF http://imgur.com/a/SJSbM http://imgur.com/a/06Ofx

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