Namal
Namal

Reputation: 2071

javascript logical operator doesn't return correct value

I have code like this.

No1 : <input type="text" name="no1">  value = 45
No2 : <input type="text" name="no2">  value = 55

<script>
      var no1 = $('input[name=no1]').val();
      var no2 = $('input[name=no2]').val();
</script>

But I check like this, it always returns false.

parseFloat(no1) < parseFloat(no2);
//false

Please explain theory behind this. I am new to javascript. I couldn't find it google either.

Upvotes: 0

Views: 59

Answers (2)

Jon Kartago Lamida
Jon Kartago Lamida

Reputation: 854

I agree with Kolink Try this.

No1 : <input type="text" name="no1">
No2 : <input type="text" name="no2">

<script>
      var no1 = document.getElementsByName('no1')[0].value;
      var no2 = document.getElementsByName('no2')[0].value;
      // will evaluate properly as long as you supply number inside input text
      parseFloat(no1) > parseFloat(no2);
</script>

Upvotes: -1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Well, as it stands right now, both inputs have an empty value, so parseFloat gives NaN, which is not comparable therefore any comparison involving it will be false.

Upvotes: 3

Related Questions