Solved

# Find minimum value with jQuery

Posted on 2010-09-20
438 Views
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
Question by:APRESTOUS
• 3
• 2

LVL 40

Expert Comment

ID: 33714761
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 Comment

ID: 33714764
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

LVL 40

Accepted Solution

gurvinder372 earned 500 total points
ID: 33714780
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 Closing Comment

ID: 33714792
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

LVL 40

Expert Comment

ID: 33714819
ohh...thanks for the tip and points
0

## Featured Post

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…