Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery function to find out max height of <li>

I want to find out that out of the given <li> which li has the maximum height? how do i write such function?

If there are n <li> elements of different size, content and height. I want to figure out the maximum height of the biggest <li> element.

like image 907
amit Avatar asked Jul 21 '11 10:07

amit


People also ask

How does jQuery calculate window height?

height() method is recommended when an element's height needs to be used in a mathematical calculation. This method is also able to find the height of the window and document. $( document ). height();

What is offsetHeight in jQuery?

offsetHeight read-only property returns the height of an element, including vertical padding and borders, as an integer. Typically, offsetHeight is a measurement in pixels of the element's CSS height, including any borders, padding, and horizontal scrollbars (if rendered).

Which jQuery method is used to show selected elements by adjusting height?

jQuery height() Method The height() method sets or returns the height of the selected elements. When this method is used to return height, it returns the height of the FIRST matched element. When this method is used to set height, it sets the height of ALL matched elements.


2 Answers

Try this:

var max = -1;
$("li").each(function() {
    var h = $(this).height(); 
    max = h > max ? h : max;
});
alert(max);
like image 62
Igor Dymov Avatar answered Sep 30 '22 18:09

Igor Dymov


If you want a one liner

var max = Math.max.apply(Math, $("li").map(function() { return $(this).height(); }))
like image 42
Dogbert Avatar answered Sep 30 '22 18:09

Dogbert