0
Button counting JS game development
So I'm trying to make a button in JS/Html, where there's an alert everytime I click on it. It should show the number increment from 0 to any number, like this: 1st click = 1 2nd click = 2 3rd click = 3 and so on How do I make this happen? My code so far was this one, but it always showed 1 when I clicked on it, nothing else: window.onload = function jump (){ let btn = document.getElementById("jump"); let count = 0; btn.onclick = jump; count += 1 alert (count) }; the "jump" ID is the button ID btw
2 odpowiedzi
+ 1
Thats because in your function you are starting again the variable count to 0. Leave count out on the global scoope.
+ 1
Yeah I got it now, I changed it to this :
window.onload = function jump (){
let btn =
document.getElementById("jump");
count = 0;
btn.onclick = function(){
count += 1;
alert (count)
};
};