Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the height of an HTML table that is dynamically filled

I would like to draw a diagram in HTML. The positioning structure looks like this:

<div id='hostDiv'>
  <div id='backgroundDiv'>
    ... drawing the background ...
  </div>
  <div id='foregroundDiv' style='position: absolute;'>
    ... drawing the foreground ...
  </div>
</div>

The foreground contains a Table element that is dynamically populated with text, hence the row heights might alter depending on the amount of text going into a cell. How can I predict the final height of the Table element in the foregroun? I need this information to set the correct height of the background. Is there a way to pre-render the Table from Javascript and read out its height? Or some other trick?

PS. The size of the hostDiv may vary as the browser resizes.

like image 200
Mr. Lame Avatar asked Dec 30 '22 09:12

Mr. Lame


2 Answers

There isn't any way to predict the height without actually rendering it in the target browser.

Once you do that, you can use (for example) jQuery to get the height of the element:

var height = $('#myTable').height();
like image 72
Adam Bellaire Avatar answered Jan 02 '23 00:01

Adam Bellaire


While using jquery, you can actually find out the height of the table before it is rendered to the user. Use a construct like this (liberally borrowing code from the person above me):

$(document).ready(function () { var height = $('#myTable').height(); });
like image 30
gx. Avatar answered Jan 01 '23 23:01

gx.