Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript update/increment variable value on click

I have this following code: JS Fiddle

<html>
    <head>
        <script>            
            function increase(){
                var a = 1;
                var textBox = document.getElementById("text");
                textBox.value = a;
                a++;
            }            
        </script>
    </head>

    <body>
        <button type="button" onclick="increase()">show</button>
        <input type="text" id="text">
    </body>
</html>​

What I am trying to do is:

  1. On clicking the button the value of 'a' will be displayed in the textbox and 'a' will be incremented.
  2. On clicking again now the incremented value should be displayed but this doesn't happen.

Where am I going wrong?

like image 484
rohan_vg Avatar asked Nov 06 '12 10:11

rohan_vg


1 Answers

<input type="text" id="text" value="1"/>
function increase(){
    var textBox = document.getElementById("text");
    textBox.value++;
}
like image 197
karaxuna Avatar answered Oct 20 '22 00:10

karaxuna