Tanase Butcaru
Tanase Butcaru

Reputation: 992

Get height of a div and add the value for another div

I want to get the height of a div and then to add that value as "top" or "margin-top" to another div. The first div has dynamical height and at its bottom I want to place another div that contains a link and a fb like button. The html code looks like this:

...
<div id="testnew">content</div>
<table>
<tr>
<td id="left">content</td>
<td id="right">
    <div id="test">content</div>
</td>
</tr>
</table>
...

So #testnew is the second div with absolute positioning and right:0;, and #test is the first div with dynamical height.

I found this script here

changed it like this:

<script type="text/javascript">
var socialNetcss = $("#test").height();
$("#testnew").css({ top: socialNetcss});
</script>

but it didn't work.

I'm a newbie in jQuery/Javascript but I really want to do this thing. Thanks in advance!

PS: I know I can directly place the second div at the firsts bottom but then I have a problem with the fb like button.

Upvotes: 0

Views: 1791

Answers (2)

uhura
uhura

Reputation: 2683

Example here:jsfiddle

<div id="first">first<br />first <br />first </div>
<div id="second"></div>​

$(document).ready(function () {
var height= $("#first").height();

$("#second").css({marginTop: height});
});​

Upvotes: 2

Raj
Raj

Reputation: 2922

Setting the "top" won't work unless your div #testnew has position style property set to absolute or relative

Try "margin-top" instead of "top" in above code

Upvotes: 0

Related Questions