Sagotharan
Sagotharan

Reputation: 2626

If condition not working in JAVASCRIPT

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

Answers (6)

Merlin
Merlin

Reputation: 4917

Try this:

if(input.val() > 0)

Upvotes: 1

user2009750
user2009750

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

user2976089
user2976089

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

Aur&#233;lien Gasser
Aur&#233;lien Gasser

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

Raul Rene
Raul Rene

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

Krish R
Krish R

Reputation: 22711

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

Upvotes: 1

Related Questions