0

I used this code to get the height of a dynamic table

var table = document.getElementById("test");
document.write(table.offsetHeight);

But now I need to use that value to set the height of a div beside that table. I don't know how to do this. I was hoping it could be done easily through the inline style of the div but I couldn't do it.

Thanks

smack-a-bro
  • 691
  • 2
  • 13
  • 27

3 Answers3

0

Since you already know how to get the table's offsetHeight, you can set it to the div by the DOM style property:

var table = document.getElementById("test");
document.getElementById("myDiv").style.height = table.offsetHeight + "px";
LcSalazar
  • 16,524
  • 3
  • 37
  • 69
0

you can do

var height=$('table').height() to get the height.

For specific div

$('div').height(height)

rather than writing table you can specify its id also. Same with div. You can specify the id of div to change the height of particular div

Tanul
  • 354
  • 4
  • 20
0

Here is an example of how to do this using jquery when the page loads:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <table id="table" style="width: 200px; border: 1px solid black; float: left;">
        <tr>
            <td>test</td>
            <td>tttttttttttttttttttttttttttttttttttttttttttttttttttttt</td>
        </tr>
        <tr>
            <td>test</td>
            <td>tttttttttttttttttttttttttttttttttttttttttttttttttttttt</td>
        </tr>
        <tr>
            <td>test</td>
            <td>tttttttttttttttttttttttttttttttttttttttttttttttttttttt</td>
        </tr>
        <tr>
            <td>test</td>
            <td>tttttttttttttttttttttttttttttttttttttttttttttttttttttt</td>
        </tr>
    </table>
    <div id="adjDiv" style="float:left; border: 1px solid red;">
        Div next to the table
    </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            //Set height using the id of my div and the height of your table
            $('#adjDiv').height($('#table').height());
        });
    </script>
</body>
</html>
swestfall
  • 411
  • 3
  • 6