get outerHeight of class and take the biggest value

1.7k Views Asked by At

I use .outerHeight to set the height of another div, using a class as selector.

var $example = $('.example');
var $height = $example.outerHeight();
var $styles = { 'height': $height }
$('.wrapper_sub').css($styles);

I want to use this on multiple "slides" of my site:

<div class="wrapper">
  <div class="example">Some Content</div>
  <div class="wrapper_sub">Other Content</div>
</div>
<div class="wrapper">
  <div class="example">Some Content</div>
  <div class="wrapper_sub">Other Content</div>
</div>
<div class="wrapper">
  <div class="example">Some Content</div>
  <div class="wrapper_sub">Other Content</div>
</div>

How can I get the .outerHeight of every .example, take only the highest value and append this to all .wrapper_sub divs?

2

There are 2 best solutions below

4
Tushar On BEST ANSWER

See comments inline:

var maxHeight = 0; // Initialize to zero
var $example = $('.example'); // Cache to improve performance

$example.each(function() { // Loop over all the elements having class example

    // Get the max height of elements and save in maxHeight variable
    maxHeight = parseFloat($(this).outerHeight()) > maxHeight ? parseFloat($(this).outerHeight()) : maxHeight;
});

$('.wrapper_sub').height(maxHeight); // Set max height to all example elements

DEMO

1
kapantzak On

Loop through the .example elements and get the max value. Then apply this value to those elements:

//Set an empty array
var arr = [];

//Loop through the elements
$('.example').each(function() {
   //Push each value into the array
   arr.push(parseFloat($(this).outerHeight()));
});

//Get the max value with sort function
var maxH = arr.sort(function(a,b) { return b-a })[0];

//Apply the max value to the '.example' elements
$('.example').css({'height': maxH + 'px'});