This works, with a simple button calling the addDamage function:
var damage=0;
function addDamage()
{
damage+=10;
document.getElementById("p1s").innerHTML=damage;
}
This doesn’t:
var damage=0;
function addDamage(who)
{
who+=10;
document.getElementById("p1s").innerHTML=who;
}
With this button:
<button type="button" onclick="addDamage(damage)">Add</button>
It’s probably obvious. I’m really new. Thanks!
Cheeso has identified the basic problem, which is that JavaScript parameters are passed by value. To get the behavior you want, you can wrap your counter in an object:
Then your button would do this:
Presumably you would have other properties for
player1that you could put in the object as well.To make the
addDamagemore flexible, you could also pass a second parameter to tell where you want to display the results:Then button looks like: