Jquery to max number from a set of divs
Given the following HTML
<div class"myclass">10</div>
<div class"myclass">25</div>
<div class"myclass">50</div>
<div开发者_StackOverflow社区 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
My shot at it:
var max = 0;
$("div.myclass").each(function(){
var value = parseInt($(this).text())
if(value > max) max = value;
});
console.log(max);
var max = 0;
$('.myclass').each(function(){
thisVal = parseInt($(this).text(), 10);
if(thisVal > max) max = thisVal;
});
alert(max);
Well, something like
var $set = $('myclass'),
max = 0;
$.each($set, function(){
if(parseInt(this.text()) > max)
max = parseInt(this.text());
});
should do it.
精彩评论