user2806214
user2806214

Reputation: 5

Comparing two variable values in jquery but getting result value 0 always

HTML CODE

<div><input type="text" disabled value="<?php echo $res['pro_quantity']; ?>" id="available"></div>

JS CODE

function quantity_check()
{
  var avail = $('#available').val();
  var quant = $('#quantity').val();
  //alert(avail)
  if (quant<=avail){
     return true;
  }
  else
    $('#quantity').val(0);
  }     

here is what when i enter any value in quantity field it changes value to 0.it seems t is not checking first condition.

Upvotes: 0

Views: 65

Answers (2)

The Process
The Process

Reputation: 5953

You need to convert those two values into number/integer, as you are comparing strings:

 var avail = parseInt($('#available').val(),10);
 var quant = parseInt($('#quantity').val(),10);  

Upvotes: 1

Dmitriy
Dmitriy

Reputation: 3765

You are compare strings in your case. Use:

var avail = +$('#available').val();
var quant = +$('#quantity').val();

Upvotes: 1

Related Questions