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

25th Jun 2022, 3:09 PM
Diana
2 ответов
+ 1
Thats because in your function you are starting again the variable count to 0. Leave count out on the global scoope.
25th Jun 2022, 3:30 PM
Frank Yohan Rodríguez Pérez
Frank Yohan Rodríguez Pérez - avatar
+ 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) }; };
25th Jun 2022, 3:36 PM
Diana