Skip to content
Advertisement

jQuery: How to calculate the maximal attribute value of all matched elements?

Consider the following HTML:

<div class="a" x="6"></div>
<div class="a" x="9"></div>
<div class="a" x="2"></div>
...
<div class="a" x="8"></div>

How would you find the maximal x value of all .a elements ?

Assume that all x values are positive integers.

Advertisement

Answer

Just loop over them:

var maximum = null;

$('.a').each(function() {
  var value = parseFloat($(this).attr('x'));
  maximum = (value > maximum) ? value : maximum;
});
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement