I want to click the button and count the quantity but it doesn't work. and error message: Uncaught ReferenceError: cnt is not defined This is my code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Make777</title> <link rel="stylesheet" href="./style.css"> </head> <body> <button type="button" class="btn-click" onclick="dongjak_button();">CLICK</button> <span>You Clicked This Button <span id="number"></span>Times!!!!!!</span> <script src="./script.js"></script> </body> </html>
"use strict"; function dongjak_button(){ cnt = 0; cnt++; document.getElementById("number").value = cnt; }
help. I want the cnt
variable to be valid. and displayed on html
You must use
var
orlet
to declare JavaScript variables.Learn more here: https://www.w3schools.com/js/js_variables.asp
The code still doesn't work because you need to get the count from
#number
first.You are in strict mode and have not declared a
cnt
variable. See MDN Documentation.You also cannot change the
value
onspan
— you needtextContent
. Also, yourcnt
is reset every time, so you need to store the variable outside the function. all in all:You Clicked This Button Times!!!!!!