Style properties undefined until I define them

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DelphiCoder
    New Member
    • Jul 2009
    • 24

    Style properties undefined until I define them

    I have the following sample:

    Code:
    <html> <body>
    <div id="mydiv" style="position: absolute;"> Hello </div>
    <script>
    alert(document.getElementById("mydiv").style.top);
    </script>
    </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).
    Last edited by Dormilich; Jul 29 '09, 08:57 AM. Reason: please use [code] tags when posting code
  • Canabeez
    New Member
    • Jul 2009
    • 126

    #2
    You can use this function to find a position of an object:
    [code=javascript]
    function findPosition(Ob ject)
    {
    if('undefined' != typeof(Object.o ffsetParent))
    {
    for(var posX=0,posY=0;O bject;Object=Ob ject.offsetPare nt)
    {
    posX += Object.offsetLe ft;
    posY += Object.offsetTo p;
    }
    return [posX,posY];
    }
    else
    {
    return [Object.x,Object .y];
    }
    }[/code]

    Comment

    Working...