Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript onclick increment number

With javascript, how can I do it so when i click a form button it adds 1 to a number? The number it increments could be in a form text field or something.

Obviously it'd be on onclick but I'm not sure of the code.

like image 445
user1022585 Avatar asked Feb 08 '12 00:02

user1022585


1 Answers

In its most basic incarnation..

JavaScript:

<script>
    var i = 0;
    function buttonClick() {
        document.getElementById('inc').value = ++i;
    }
</script>

Markup:

<button onclick="buttonClick()">Click Me</button>
<input type="text" id="inc" value="0"></input>
  • DEMO
like image 80
calebds Avatar answered Oct 07 '22 00:10

calebds