Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate max value and set background to equal parent

im trying now to:

  1. Get number from text DONE
  2. Set it to equal for this value DONE
  3. Get a value a get max DONE

4. and set the max value parent background to for example background: #000;

So my question is how i gonna get a parent of the MAX value what was calculated before?

CODEPEN DEMO

$('.views').each(function(){
    valText = $(this).text();
    $(this).attr('value', valText);
});

var numbers = $(".views").map(function(){
    return parseFloat(this.getAttribute('value')) || -Infinity;
});

var calculate = Math.max.apply(Math, numbers);
$("#max").html(calculate);
<div><div class="views" >10</div></div>
<div><div class="views" >1</div></div>
<div><div class="views" >7</div></div>
<div><div class="views" >5</div></div>
<div><div class="views" >3</div></div>

Max value is:

<p>Max value is: <span id="max"></span></p>
like image 930
liborza Avatar asked Dec 10 '25 00:12

liborza


1 Answers

Being a little tricky, reducing the collection to the one with the highest number inside

var el = $('.views').toArray().reduce(function(a,b) {
    return (+$(a).text()) > (+$(b).text()) ? $(a) : $(b);
});

FIDDLE

To get the parent, simply

el.parent();
like image 173
adeneo Avatar answered Dec 12 '25 12:12

adeneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!