Reputation: 15
Alright, I will put this out there now, my knowledge of programming isn't super great. Only been programming for a few months.
I've been interested in making a browser based game and am currently just playing around with the code to see how things work.
I have both HTML and Javascript in my code. I am trying to get it so that when ButtonA is pressed, VariableA increases by set amount. I've done this before on other small programs I've done, so I simply did exactly what I did on the other programs, but for some reason, when the button is clicked, the variable is not changed.
My button is defined in the HTML code, and the function is run from the javascript. I'll put the code here so you can see for yourselves.
<script type="text/javascript">
//DEFINED VARIABLES
var playerLevel = 1;
var playerExp = 0;
var playerGold = 0;
var playerCredits = 0;
var neededExp = 30;
//FIGHT BUTTON
function Fight() {
playerExp += 5;
document.getElementById("playerExp").innerHTML = playerExp;
}
</script>
That is all the Javascript code my program currently has. The function setup looks perfectly fine to me. Now here is the HTML code for the button setup.
<h3>
<button type="button" onClick="Fight"><background color="gray"><font color="black">Fight!</font></button>
</h3>
The button is the same way I have always done buttons and had them working. I have it put into the h3 tag because I placed the button on the page using my stylesheet. For which the code I have is:
h3 {
position: fixed;
top: 283px;
left: 20px;
}
I know I am probably missing something simple, but as far as I can tell everything looks to be setup exactly how my other games buttons have been setup. The issue that I can't figure out is when the button is pressed, the variable playerEXP does not increase, or, if it is increasing, it's not updating the variable display. I also had my friend who is in the same class as me look it over and he said it looked fine as well.
Been at it over an hour, any help is greatly appreciated.
Upvotes: 0
Views: 466
Reputation: 757
call function in paranthesis
<h3>
<button type="button" onClick="Fight();"><background color="gray"><font color="black">Fight!</font></button>
</h3>
Upvotes: 2