Basically I want to increase a value each time a button is clicked using only Javascript/html/css. I’ve seen topics like this but nothing exactly like what I’m looking for. The idea is to simple add +1 each time a button is clicked and I can’t figure it out. Basically like cookie clicker, except very very simple.
function call() {
g = 1;
x = 1;
v = x + g + 1;
document.getElementById("cash").innerHTML = "Your money: "+ v;
}
[quote=“J0sh, post:1, topic:208465, full:true”]
Basically I want to increase a value each time a button is clicked using only Javascript/html/css.[/quote]
Move the variables out of the function, and you’re good to go.
var g = 1,
x = 1,
v = g + x;
function call() {
v += 1;
document.getElementById("cash").innerHTML = "Your money: "+ v;
}
Okay, thanks. How does that work though? I now have:
<script> v = 0 + 0; function call() { v += 1; document.getElementById("cash").innerHTML = "Your money: $"+ v; } </script>
Which works perfectly fine but I’m not sure I understand how. What I understand is happening: v starts as 0. each time the button is clicked the function is called which adds 1 to v. How come v has to be outside though? And is there any way to save this instead of reloading afterwards and losing it all? Thanks again
If it is defined / assigned an initial value inside the function it would in effect be “reset” back to that initial value every time the function runs.
Your topic title says “only JavaScript” without the value being server-side in some way there is only so much you can do.
Do you consider cookies as “only JavaScript”?
* note, if a cookie is refused / deleted you will still “lose it all”