Solved

Find minimum value with jQuery

Posted on 2010-09-20
5
438 Views
Last Modified: 2012-05-10
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
Comment
Question by:APRESTOUS
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:gurvinder372
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

by:APRESTOUS
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

by:
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  );

Open in new window

0
 

Author Closing Comment

by:APRESTOUS
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

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now