Link to home
Start Free TrialLog in
Avatar of APRESTOUS
APRESTOUS

asked on

Find minimum value with jQuery

I have a table with all cells having unique id as "td_1", "td_2" etc.
I want to find which cell contains minimal value  (content of cells are integers  or "---" )
I use jQuery, so probably the solution will use it as well.
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

var maxValue = 0;
var minValue = 0;
$("td[id^='td_']").each(function(){
   var val = $(this).html();
   
    if ( val != "---" )
    {
        val = parseInt( val );
         if ( val <  minValue  )
        {
              minValue  = val;
        }
         if ( val >  maxValue )
        {
              maxValue = val;
        }
    }
});
alert("min value is: " + minValue  );
alert("max value is: " + maxValue );
Avatar of APRESTOUS
APRESTOUS

ASKER

Hello,
thank you for very quick answer.
You solution find the minimum value.
But I need to know actually which cell contains this value.
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!
It works.
Only one remark.
If I use it so I get always min. value = 0.
But I changed second string into

var minValue = 999999999;

and it returns exactly what I meant.
ohh...thanks for the tip and points