Bayu Sanjaya
Bayu Sanjaya

Reputation: 7

How to get highest value text while field in same class, id and name?

I want to get value from highest while text field in the same class I have tried to make a script like this but it does not work.

<input class="resbobot" name="BobotY2" id="BobotY2" type="text"value="100">
<input class="resbobot" name="BobotY3" id="BobotY3" type="text"value="80">
<input class="resbobot" name="BobotY4" id="BobotY4" type="text"value="70">

JS

$(".resbobot").each(function() {
   if ($(this).val()===100) { 
       alert($(this).val()); 
   }

Upvotes: 0

Views: 779

Answers (5)

Khan
Khan

Reputation: 439

To find the highest value from the input fields using jQuery, use the following code:

var highestVal = null;
$('.resbobot').each(function(){
    var curVal = Number($(this).val());
    highestVal = (highestVal === null || highestVal < curVal) ?  curVal : highestVal;        
});   

alert(highestVal);

The above code will work even if the input values are all negative numbers.

Upvotes: 0

Babak Naffas
Babak Naffas

Reputation: 12561

The === operator compares value and type. Your code is comparing the string literal '100' against the number 100. You can use the == operator to ignore the type or use parseInt(..) as @RGS suggested.

Upvotes: 1

Blue
Blue

Reputation: 463

=== Checks type and value, == checks value. So "100" === 100 returns false where "100" == 100 returns true.

Upvotes: 0

RGS
RGS

Reputation: 5211

$(".resbobot").each(function() {
 if(parseInt($(this).val())===100)
 {
  alert($(this).val());
 }
});

You have to check text box value with string data type i.e. ' ' or else with integer data type because you are using equal value and equal type operator.

Demo:

http://jsfiddle.net/h9jap3gp/1/

Upvotes: 0

quux
quux

Reputation: 918

var max = 0
$(".resbobot").each(function() { if ($(this).val()>max) { max = $(this).val()}) 
alert(max)

Upvotes: 0

Related Questions