2

I have one div and more than one data comes into this div by jstl tag. I want to find max content div height. I have seen one link but it is show in alert every time 20. element with the max height from a set of elements

JSP

     <div id="div-height" class='cat-product-name'>${i.name} </div>

JAVA SCRIPT

  $(window).load(function () {
  var maxHeight = Math.max.apply(null, $("#div-height").map(function ()
                {
                    return $(this).height();
                }).get());
                alert(maxHeight);
});

enter image description here

I want to find max height of div and set this height of every div.

Community
  • 1
  • 1
Varun Sharma
  • 4,632
  • 13
  • 49
  • 103

3 Answers3

6

You can try this:-

$(document).ready(function() {
  var maxHeight = -1;

  $('.cat-product-name').each(function() {
    maxHeight = maxHeight > $(this).height() ? maxHeight :     $(this).height();
 });

 $('.cat-product-name').each(function() {
   $(this).height(maxHeight);
 });
});

Reference - Use jQuery/CSS to find the tallest of all elements

Community
  • 1
  • 1
khushboo29
  • 906
  • 6
  • 16
  • thanks for save my time. If u like my answer then please voting up to help others. – Varun Sharma Feb 20 '16 at 09:59
  • 1
    Yes it will work on images, it all depends on which all elements height you are concerned with and then set image(s) height the max value - $(this).find("img").height(maxHeight); – khushboo29 Feb 20 '16 at 10:24
0

Each element id on the page must be unique, i.e. you can't have multiple elements with

id="div-height"

Try using class instead (class="div-height"). Note you'll have to adjust your jQuery selector as well to

$(".div-height")
JON
  • 965
  • 2
  • 10
  • 28
Emmett
  • 14,035
  • 12
  • 56
  • 81
0

A modern take to this problem is the usage of css flex-box with align-items: stretch; :

.container {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  
  width: 250px;
}

.container>div {
  flex: 0 0 100px;
  outline: 5px solid #888;
  padding: 10px;
}
<div class="container">
  <div>Small content</div>
  <div>This divs content needs more height than their siblings!</div>
  <div>Some more text</div>
  <div>Other text</div>
</div>
Simon
  • 1,172
  • 12
  • 21