Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect the widest cell w/ jQuery

Tags:

jquery

I have a table with different values. First column contains labels. I need to get the width of the widest label. I assume I need some sort of a loop, but then what?

$("#myTable td:nth-child(1)").width();

Thanks.

like image 277
santa Avatar asked Dec 22 '22 09:12

santa


2 Answers

var w = 0;
$("#myTable tr td:first").each(function(){
    if($(this).width() > w){
        w = $(this).width();
    }
});
like image 135
Marek Sebera Avatar answered Jan 09 '23 14:01

Marek Sebera


I assume that you have one <label> element inside all <td> elements in the first column (since it makes no sense to compare the widths of the <td> elements themselves — within the same column they are equally wide (not considering colspan != 1)):

var widestLabel = 0;
$("#myTable td:nth-child(1) label").each(function() {
    widestLabel = Math.max(widestLabel, $(this).width());
});
like image 40
jensgram Avatar answered Jan 09 '23 14:01

jensgram