10

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?

10 Answers 10

13

You're only incrementing a local variable that goes away at end of function. You may do this :

      var a = 1;
      function increase(){
            var textBox = document.getElementById("text");
            textBox.value = a;
            a++;
      }    
Sign up to request clarification or add additional context in comments.

3 Comments

+1; note that polluting the global namespace is not recommended. You can avoid a global variable by using a self-invoking function.
how to maintain the incremented value in var a .
does this add the increment at the end of the function??
8
<input type="text" id="text" value="1"/>
function increase(){
    var textBox = document.getElementById("text");
    textBox.value++;
}

1 Comment

shorter way.. liked that.. +1 for this
0

It's better to check the value of your text field. If it is empty a=1 else textfield.value++

Comments

0

i meant var a=1; should be declared before the function

1 Comment

"you need a++" and "var a=1; should be declared before the function" don't seem very similar.
0

you can use this.Just copy and paste

    <html>
    <head>
        <script type="text/javascript">  
        var a = 1;          
            function increase(){


                document.getElementById("text").value=a;
                a=a+1;

            }            
        </script>
    </head>

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

Comments

0

use this code

    var k = 1;
    function shashi() {
        document.getElementById('para').innerHTML = k;
        k++;
    }
    k = k;
</script><br/>

and call shashi() function on click event

Comments

0

You should practice not to declare global variables until it necessary. It leads a hole in big applications sometime. JS functions are also objects that means they can have properties.

<html>
    <head>
        <script>            
            function increase(){
                if( typeof this.counter == 'undefined' ) {
					        this.counter = 1;
				        }
				
                var textBox = document.getElementById("text");
                textBox.value = this.counter;
                this.counter++;
            }            
        </script>
    </head>

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

Comments

0
 <input type="text" id="text" value="1" />
<p id="plus">+</p>
<script>
    const plus = document.getElementById('plus');
    plus.addEventListener('click', function() {
        var textBox = document.getElementById("text").value;
        let inputNumber = parseFloat(textBox);
        console.log(inputNumber);
        inputNumber += 1;
        document.getElementById("text").value = inputNumber;
    })
</script>

1 Comment

Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes
0

You have put on quotes before & after the function increase() in the button coding too

1 Comment

I am unclear what you mean. Either "... but you should not. See this code how to avoid it." or "You have to put on ...". Maybe if you add either code or an explanation of what is wrong, why, how to solve it and how that works, then things might be clearer.
-1

You have a simpler approach implementing increment and decrement buttons. Quick example using inline javascript:

<!-- increment button -->
<input type='button' name='add' onclick='javascript: document.getElementById("qty").value++;' value='+'/>
<!-- quantity input -->
<input name='qty' id='qty' style="text-align:center;" type="text" value="1"  autocomplete="off" size="2">
<!-- decrement button -->
<input type='button' name='subtract' onclick='javascript: document.getElementById("qty").value--;' value='-'/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.