Javascript - How to program a click counter?

button, javascript

Solution

you need to move the line `var x = 0;` to somewhere outside of the function `count` in order for it to be in the global scope. This means that changes made to it by the function `count` will persist.

e.g.

var x = 0;
function count() {
    x += 1;
    document.getElementById( "counting" ).value = x;
}

Problem

I have tried before, but it doesn't work. Is it any wrongs of the code? ``` <script type ="text/javascript"> function count() { var x = 0; x += 1; document.getElementById( "counting" ).value = x; } </script> </head> <body> <input type ="button" value = "Click" onclick = "count()"/><br><br> <input id = "counting" type = "text" /> </body> ```

Original source