tags:

views:

47

answers:

3

Guys,

Given the following HTML

<div class"myclass">10</div>
<div class"myclass">25</div>
<div class"myclass">50</div>
<div class"myclass">20</div>

I want Jquery to return the maximum value found on divs with class:"myclass". (This is 50)

I thought of using .find.text() will be a good starting point but cant figure out exactly how,

Help is greatly appreciatted,

Thanks

+1  A: 
var max = 0;
$('.myclass').each(function(){
    thisVal = parseInt($(this).text(), 10);
    if(thisVal > max) max = thisVal;
});
alert(max);
Ben
Using $(this).val() doesn't work on a div
Mike Robinson
You can use .text() instead.
Austin Fitzpatrick
When you use `parseInt` you should set the radix to 10, for this case: `thisVal = parseInt($(this).text(),10);`
fudgey
A: 

well, something like

var $set = $('myclass'),
    max  = 0;
$.each($set, function(){
    if(parseInt(this.text()) > max)
       max = parseInt(this.text());
});

should do it.

Kind Regards

--Andy

jAndy
+2  A: 

My shot at it:

var max = 0;
$("div.myclass").each(function(){
    var value = parseInt($(this).text())
    if(value > max) max = value;
});
console.log(max);
Mike Robinson
+1 for using div.myclass as the selector
James Westgate