Solved

Find minimum value with jQuery

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

786 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