Javascript Form How to - Create click counter by text input








Question

We would like to know how to create click counter by text input.

Answer


<!DOCTYPE html>
<html>
<body>
  <input id="input" type="text" value="10">
  <a id="btn" href="#">click me</a>
<!--from w  w w.  j a  v  a2s. co m-->
<script type='text/javascript'>

    document.getElementById('btn').onclick = function() {
      var input = document.getElementById('input');
      input.value = parseInt(input.value) - 1;
    };

</script>
</body>
</html>

The code above is rendered as follows: