element with the max height from a set of elements
JqueryJquery Problem Overview
I have a set of div
elements. In jQuery
, I would like to be able to find out the div
with the maximum height and also the height of that div
. For instance:
<div>
<div class="panel">
Line 1
Line 2
</div>
<div class="panel">
Line 1<br/>
Line 2<br/>
Line 3<br/>
Line 4<br/>
</div>
<div class="panel">
Line 1
</div>
<div class="panel">
Line 1
Line 2
</div>
</div>
By looking at the above, we know that the 2nd div
(with 4 Lines) has the maximum height of all. How do I find this out? Could someone please help?
So far I've tried:
$("div.panel").height()
which returns the height of the 1st div
.
Jquery Solutions
Solution 1 - Jquery
var maxHeight = Math.max.apply(null, $("div.panel").map(function ()
{
return $(this).height();
}).get());
If that's confusing to read, this might be clearer:
var heights = $("div.panel").map(function ()
{
return $(this).height();
}).get();
maxHeight = Math.max.apply(null, heights);
Solution 2 - Jquery
The html that you posted should use some <br>
to actually have divs with different heights. Like this:
<div>
<div class="panel">
Line 1<br>
Line 2
</div>
<div class="panel">
Line 1<br>
Line 2<br>
Line 3<br>
Line 4
</div>
<div class="panel">
Line 1
</div>
<div class="panel">
Line 1<br>
Line 2
</div>
</div>
Apart from that, if you want a reference to the div with the max height you can do this:
var highest = null;
var hi = 0;
$(".panel").each(function(){
var h = $(this).height();
if(h > hi){
hi = h;
highest = $(this);
}
});
//highest now contains the div with the highest so lets highlight it
highest.css("background-color", "red");
Solution 3 - Jquery
If you want to reuse in multiple places:
var maxHeight = function(elems){
return Math.max.apply(null, elems.map(function ()
{
return $(this).height();
}).get());
}
Then you can use:
maxHeight($("some selector"));
Solution 4 - Jquery
function startListHeight($tag) {
function setHeight(s) {
var max = 0;
s.each(function() {
var h = $(this).height();
max = Math.max(max, h);
}).height('').height(max);
}
$(window).on('ready load resize', setHeight($tag));
}
jQuery(function($) {
$('#list-lines').each(function() {
startListHeight($('li', this));
});
});
#list-lines {
margin: 0;
padding: 0;
}
#list-lines li {
float: left;
display: table;
width: 33.3334%;
list-style-type: none;
}
#list-lines li img {
width: 100%;
height: auto;
}
#list-lines::after {
content: "";
display: block;
clear: both;
overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<ul id="list-lines">
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="" />
<br /> Line 1
<br /> Line 2
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="" />
<br /> Line 1
<br /> Line 2
<br /> Line 3
<br /> Line 4
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="" />
<br /> Line 1
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="" />
<br /> Line 1
<br /> Line 2
</li>
</ul>
Solution 5 - Jquery
ul, li {
list-style: none;
margin: 0;
padding: 0;
}
ul {
display: flex;
flex-wrap: wrap;
}
ul li {
width: calc(100% / 3);
}
img {
width: 100%;
height: auto;
}
<ul>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="">
<br> Line 1
<br> Line 2
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="">
<br> Line 1
<br> Line 2
<br> Line 3
<br> Line 4
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="">
<br> Line 1
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="">
<br> Line 1
<br> Line 2
</li>
</ul>
Solution 6 - Jquery
Try https://stackoverflow.com/questions/2313140/find-out-divs-height-and-setting-div-height (Similar to the one Matt posted but using a loop)
Solution 7 - Jquery
If you were interested in sorting entirely in standard JavaScript, or without using forEach():
var panels = document.querySelectorAll("div.panel");
// You'll need to slice the node_list before using .map()
var heights = Array.prototype.slice.call(panels).map(function (panel) {
// return an array to hold the item and its value
return [panel, panel.offsetHeight];
}),
// Returns a sorted array
var sortedHeights = heights.sort(function(a, b) { return a[1] > b[1]});
Solution 8 - Jquery
Easiest and clearest way I'd say is:
var maxHeight = 0, maxHeightElement = null;
$('.panel').each(function(){
if ($(this).height() > maxHeight) {
maxHeight = $(this).height();
maxHeightElement = $(this);
}
});
Solution 9 - Jquery
This might be helpful, in some way. Fixing some code from @diogo
$(window).on('load',function(){
// get the maxHeight using innerHeight() function
var maxHeight = Math.max.apply(null, $("div.panel").map(function () {
return $(this).innerHeight();
}).get());
// Set the height to all .panel elements
$("div.panel").height( maxHeight );
});