Reputation: 2626
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);}
})
Upvotes: 2
Views: 132
Reputation: 3187
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);}
})
Upvotes: 1
Reputation: 355
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
Upvotes: 2
Reputation: 3120
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)
Upvotes: 1
Reputation: 10270
You are comparing a HTML element with 0. You need to compare its value with 0
Try
if (input.val() > 0)
Upvotes: 2