• Status: Solved
• Priority: Medium
• Security: Public
• Views: 465

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.
0
APRESTOUS
• 3
• 2
1 Solution

Commented:
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 );
0

Author Commented:
Hello,
thank you for very quick answer.
You solution find the minimum value.
But I need to know actually which cell contains this value.
0

Commented:
you mean the id of that cell is required, right?

``````var maxValue = 0;
var minValue = 0;
var idOfMinCell;
var idOfMaxCell;
\$("td[id^='td_']").each(function(){
var val = \$(this).html();

if ( val != "---" )
{
val = parseInt( val );
if ( val <  minValue  )
{
minValue  = val;
idOfMinCell = \$(this).attr("id");
}
if ( val >  maxValue )
{
maxValue = val;
idOfMaxCell = \$(this).attr("id");
}
}
});

alert("min value is: " + minValue  );
alert("max value is: " + maxValue );
alert("id of min cell value is: " + idOfMinCell  );
alert("id of max cell value is: " + idOfMaxCell  );
``````
0

Author Commented:
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.
0

Commented:
ohh...thanks for the tip and points
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.