Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the largest table cell height with jQuery?

I have a table with cells of variable-length text-content. I want to find the height of the tallest cell and then make all the cells that height. How can I do this?

like image 963
sova Avatar asked Nov 08 '10 19:11

sova


2 Answers

Like this:

var max = 0;    
$('table td').each(function() {
    max = Math.max($(this).height(), max);
}).height(max);

In plain english, loop through all the cells and find the maximum value, then apply that value to all the cells.

like image 187
Tatu Ulmanen Avatar answered Oct 08 '22 16:10

Tatu Ulmanen


Just to be different:

var heights = $('td').map(function() {
    return $(this).height();
}).get();

var maxHeight = Math.max.apply(Math, heights);

$('td').css('height', maxHeight);
like image 43
John Strickler Avatar answered Oct 08 '22 14:10

John Strickler