Abbas
Abbas

Reputation: 5044

How to get the height of the div dynamically

i have 2 div's, left and right, the content in right div is setted dynamically so as its height, so i haven't specified its height in html, now what i want is the height of the left div should be equal to the right div, how to set the height of the left div as per the height of right div, using jquery, i tried

$("#leftDiv").height($("RightDiv").height());

this does not seems working, as i haven't specified the height of the right div in html. is there any other wayout, apart from using tables.

Upvotes: 5

Views: 20777

Answers (4)

Aram Mkrtchyan
Aram Mkrtchyan

Reputation: 2700

$(function() {
 if($("#RightDiv").height() > $("#leftDiv").height()){    
           $("#leftDiv").css("height", $("#RightDiv").height() + "px");
        } else {
           $("#rightDiv").css("height", $("#leftDiv").height() + "px");
       }
}

Upvotes: 0

dknaack
dknaack

Reputation: 60438

Description

Looks like your selector for RightDiv is not right or you forgot to wait while the DOM is loaded.

Sample

Html

<div id="leftDiv" style="border:1px solid red">left div</div>
<div id="RightDiv" style="height:100px; border:1px solid red">right div</div>

jQuery

$(function() {
   $("#leftDiv").height($("#RightDiv").height());
})

More Information

Upvotes: 2

Teun Pronk
Teun Pronk

Reputation: 1399

try

var firstHeight = $("#RightDiv").height() + 'px';
$("#leftDiv").css("height", firstHeight);

Upvotes: 0

Alex Dn
Alex Dn

Reputation: 5553

Try this:

$("#leftDiv").height($("#RightDiv").get(0).scrollHeight);

Upvotes: 1

Related Questions