Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get height of a div and set the height of another div using it

Tags:

javascript

I have two divs, neither have a height set in css, as I want to take whatever height they end up as and set the other div to be that height.

The javascript I have is this

function fixHeight() {
    var divh = document.getElementById('prCol1').height;
    document.getElementById('prCol1').innerHTML = '<ul><li>' + divh + '</li></ul>';
    document.getElementById('prCol2').style.height = divh + 'px';
}

I have the following line of code just to see if I am getting some kind of actual response.

document.getElementById('prCol1').innerHTML = '<ul><li>' + divh + '</li></ul>';

I have a onload set to run the function

my two divs look like this

<div id="prCol1">
     ..content..
</div>
<div id="prCol2" class="slideshow">
    ..content..
</div>
like image 478
BrettAdamsGA Avatar asked Jun 12 '12 20:06

BrettAdamsGA


3 Answers

Use offsetHeight - http://jsfiddle.net/HwATE/

function fixHeight() {
    var divh = document.getElementById('prCol1').offsetHeight; /* change this */
    document.getElementById('prCol1').innerHTML = '<ul><li>' + divh + '</li></ul>';
    document.getElementById('prCol2').style.height = divh + 'px';
}
like image 166
Zoltan Toth Avatar answered Oct 05 '22 15:10

Zoltan Toth


You can use jQuery to get the height and set it.

var h = $("#prCol1").height();
$("#prCol2").height(h);
like image 42
Ryan Avatar answered Oct 05 '22 15:10

Ryan


you can get height by this code:

document.getElementById('YourElementID').clientHeight;
like image 21
S. V Avatar answered Oct 05 '22 13:10

S. V