Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

element with the max height from a set of elements

Tags:

jquery

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> &lt/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.

like image 420
lshettyl Avatar asked May 19 '11 15:05

lshettyl


People also ask

Which of the following properties is used to set the maximum height for an element?

The max-height CSS property sets the maximum height of an element.

How do you define maximum height?

The max-height property defines the maximum height of an element. If the content is larger than the maximum height, it will overflow. How the container will handle the overflowing content is defined by the overflow property. If the content is smaller than the maximum height, the max-height property has no effect.

What is the difference between min-height and max-height?

The max-height and min-height properties are used to set the maximum and minimum height of an element. This prevents the value of the height property from becoming larger than max-height or smaller than min-height. The value of the max-height and/or min-height property overrides height.

How do you use maximum height?

The max-height property in CSS is used to set the maximum height of a specified element. Authors may use any of the length values as long as they are a positive value. max-height overrides height, but min-height always overrides max-height .


1 Answers

Use .map() and Math.max.

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); 
like image 174
Matt Ball Avatar answered Oct 06 '22 23:10

Matt Ball