开发者

Jquery to max number from a set of divs

开发者 https://www.devze.com 2022-12-25 22:42 出处:网络
Given the following HTML <div class\"myclass\">10</div> <div class\"myclass\">25</div>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消