2

I have three buttons, + value -. in this when i click + button value will increase and - value will be decrease .. i have wote the code in jSFIDLLE. it working.

And the value should not be minus. so i added one if condition. then my code is not working.

html-

<input type="Button" id='AddButton' value="+" />
<input type="Button" id='BoqTextBox' value="0" />
<input type="Button" id='MinusButton' value="-" />

javascript -

$('#AddButton').on('click', function () {
    var input = $('#BoqTextBox');
    input.val(parseFloat(input.val(), 10) + 1);
})
$('#MinusButton').on('click', function () {    
  var input = $('#BoqTextBox');
    if(input>0)
    { input.val(parseFloat(input.val(), 10) - 1);}
})
3
  • 1
    jsfiddle.net/sagotharan/J4Jxa/1 Commented Dec 9, 2013 at 7:33
  • 2
    input is an object, a jQuery object. What makes you think it will be greater than 0? Commented Dec 9, 2013 at 7:36
  • use .val() property of input Commented Dec 9, 2013 at 7:44

6 Answers 6

2

You are comparing a HTML element with 0. You need to compare its value with 0

Try

if (input.val() > 0)
Sign up to request clarification or add additional context in comments.

Comments

2

You need to use

if(input.val() > 0)

As

if(input > 0) 

Means that you are trying to see if the object, not it's value is bigger than 0, which is not the case as it is an element

Comments

1

Use if(input.val()>0) instead of if(input>0)

Comments

1

You are trying to compare input with 0. You should instead compare the value of input with 0.

Replace

if (input > 0)

with

if (parseInt(input.val()) > 0)

2 Comments

is parseInt need to add,. input.val() gives output, and parseInt(input.val() also. what is the use of parseInt?
The difference is the type of the result: input.val() equals the string "1" whereas parseInt(input.val()) equals the number 1. It works if you omit to call parseInt() because javascript is smart : when it compares the string "1" and the integer 0, it understands that the string is, in fact, an integer.
1

Here it is

$('#AddButton').on('click', function () {
    var input = $('#BoqTextBox');
    input.val(parseFloat(input.val(), 10) + 1);
})
$('#MinusButton').on('click', function () {    
  var input = $('#BoqTextBox');
    if(input.val()>0)
    { input.val(parseFloat(input.val(), 10) - 1);}
})

Comments

1

Try this:

if(input.val() > 0)

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.