Reputation: 2164
I am working on a function.
var price = $("input[name=bid_price]").val();
var milestone = $("input[name=initial_price]").val();
if(price >= milestone)
{
console.log('True);
}
else {
console.log('False');
}
Now, if I enter price as 100 and milestone as 90. Then, it should print True, but it goes to else and prints false. If I change greater than to less than which means 100 < 90, then it prints true. What is wrong here, why it is not working?
Upvotes: 0
Views: 175
Reputation: 23859
You must parse the retrieved values to number first:
var price = parseFloat($("input[name=bid_price]").val());
var milestone = parseFloat($("input[name=initial_price]").val());
Values retrieved from input boxes using val()
are strings. Thus the logical comparison become buggy. If you convert those into numbers, then your code will run fine.
See this working demo:
var price = parseFloat($("input[name=bid_price]").val());
var milestone = parseFloat($("input[name=initial_price]").val());
if (price >= milestone) {
console.log('True');
} else {
console.log('False');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="bid_price" value="46">
<input name="initial_price" value="27">
Upvotes: 3