Reputation: 252
I'm using Jquery Tab, I have table for each tab. How can I get height of ALL table simultaneously(not to get table height per active tab using event like click) .
$(function() {
$( "#tabs" ).tabs();
});
alert("Table 1 Height is : "+ $("#table1").height());
alert("Table 2 Height is : " +$("#table2").height());
Note,
See Fiddle
Upvotes: 1
Views: 527
Reputation: 1608
A way is to walk over the tabs and initially activate/deactivate them:
$(function() {
var tableHeights = {};
$( "#tabs" ).tabs();
$( "#tabs" ).tabs('widget').children('div').each(function(i) {
var table = $(this).find('table');
$( "#tabs" ).tabs('option', 'active', i);
tableHeights[ table.attr('id') ] = table.height();
});
$( "#tabs" ).tabs('option', 'active', 0);
alert("Table 1 Height is : "+ tableHeights['table1']);
alert("Table 2 Height is : "+ tableHeights['table2']);
});
See the edited Fiddle
The advantage compared to Wenceslao Negrete's answer is that the tabs switch and not just the content. But there is no way of doing it without the flashing content on each page load. The cleaner way requires the assurance that all table cells have the same height. No content is needed to be visible this way but if the height of the rows differ it won't give the desired result:
var rowHeight;
$( "#tabs" ).tabs('widget').children('div').each(function(i) {
var table = $(this).find('table');
if (i === 0) {
// assuming on load the active tab is always the first
rowHeight = table.find('td:first').height();
}
tableHeights[ table.attr('id') ] = table.find('tr').length * rowHeight;
});
Upvotes: 0
Reputation: 556
Like the comment after your post said, there is no "API way" to get what your looking for, but you can use this code snippet, to achieve your purpose.
$(function() {
$( "#tabs" ).tabs();
$('#tabs div').each(function(index){
var display = $(this).css('display');
$(this).css('display', 'block');
table = $(this).find('table');
alert("Table "+ index +" Height is : "+ table.height());
$(this).css('display',display);
});
});
Upvotes: 1