Solved

Find minimum value with jQuery

Posted on 2010-09-20
5
448 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

820 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