Connecting Tech Pros Worldwide Forums | Help | Site Map

Style properties undefined until I define them

Newbie
 
Join Date: Jul 2009
Posts: 24
#1: Jul 28 '09
I have the following sample:

Expand|Select|Wrap|Line Numbers
  1. <html> <body>
  2. <div id="mydiv" style="position: absolute;"> Hello </div>
  3. <script>
  4. alert(document.getElementById("mydiv").style.top);
  5. </script>
  6. </body> </html>
The resulting message box shows nothing. If I add "top: 10px" to the style (in the div tag), then it shows "10px". That means that I can't read the top/left/height/width properties without specifying them first. But I need to position another div based on the location/size of this div, which I don't want to specify explicitly (I'm using 'float' property and its height changes based on its content). Is there a way to do this?

I basically have two divs, "float: left" for both. The height of the one on the left is determined by its content, and I need to force the height of the one on the right to be exactly the same as the one on the left (I'm turning the scroll bars on for it).

Canabeez's Avatar
Member
 
Join Date: Jul 2009
Location: Israel
Posts: 85
#2: Jul 29 '09

re: Style properties undefined until I define them


You can use this function to find a position of an object:
Expand|Select|Wrap|Line Numbers
  1. function findPosition(Object)
  2. {
  3.     if('undefined' != typeof(Object.offsetParent))
  4.     {
  5.         for(var posX=0,posY=0;Object;Object=Object.offsetParent)
  6.         {
  7.             posX += Object.offsetLeft;
  8.             posY += Object.offsetTop;
  9.         }
  10.         return [posX,posY];
  11.     }
  12.     else
  13.     {
  14.         return [Object.x,Object.y];
  15.     }
  16. }
Reply