PDA

View Full Version : Quick offsetHeight Question



mosarani
Sep 16th, 2007, 04:54 PM
I'm trying to get the height of a <div> tag so I can use it as a reference in a function to change image height, but I can't seem to get it.


function getdimensions()
{
RightContent = document.getElementById("Right").offsetHeight;
LeftContent = document.getElementById("Left").offsetHeight;
return LeftContent, RightContent;
}
window.onload = function() {
getdimensions();
}


I have that, and the div tags have the corresponding ID's, but when I try one of these html buttons to test I just get 'undefined'.


<button onclick="alert(RightContent.offsetHeight);">Right Height</button><br>

<button onclick="alert(LeftContent.offsetHeight);">Left Height</button>

What's going wrong?

BarrMan
Sep 16th, 2007, 06:11 PM
Are RightContent and LeftContent global variables?

mosarani
Sep 16th, 2007, 06:41 PM
In Html context it's

<script language="javascript">

var RightContent = 0
var LeftContent = 0

function getdimensions()
{
RightContent = document.getElementById("Right").offsetHeight;
LeftContent = document.getElementById("Left").offsetHeight;
}
window.onload = function() {
getdimensions();
}

</script>

Whether or not I define the variables outside of the function doesn't change the result, and it shouldn't because the variables are declared as global within the function anyways.

BarrMan
Sep 16th, 2007, 06:45 PM
Try this:

<button onclick="alert(RightContent);">Right Height</button><br>

<button onclick="alert(LeftContent);">Left Height</button>

Since you defined these variables as the object's offsetHeight it would become document.getElementById("Left").offsetHeight.offsetHeight.

mosarani
Sep 16th, 2007, 10:07 PM
*beats head on wall*

Many thanks sir.

BarrMan
Sep 16th, 2007, 10:15 PM
No problem. Glad I could help.