Search code examples
javascriptjqueryhtmlouterheight

get outerHeight of class and take the biggest value


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?


Solution

  • 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