?
Solved

Find minimum value with jQuery

Posted on 2010-09-20
5
Medium Priority
?
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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:
Gurvinder Pal Singh earned 2000 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:Gurvinder Pal Singh
ID: 33714819
ohh...thanks for the tip and points
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

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