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?
Try this:
var widest;
var widestWidth = 0;
$("#myTable td").each(function(t) {
if($(this).width() > widestWidth){
widest = $(this);
widestWidth = $(this).width();
}
});
//Now widest points to the widest element
Note I tracked the actual width separately from the widest element. An alternate solution without this method would be to initialize widest to a dummy element with width = 0 and just compare $(this).width() with widest.width()
EDIT: Or, alternately, now that I realize you wanted only the width and not the actual element, you could use this version:
var widestWidth = 0;
$("#myTable td").each(function(t) {
widestWidth = Math.max(widestWidth, $(this).width());
});