Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts

    element position

    I am very surprised to see that I can not use offset postion to find the real position of an element:

    <script language="JavaScript" type="text/JavaScript">
    onload=function(){alert(document.getElementById('myid').offsetLeft)}
    </script>
    ...
    <body>
    <div id="myid" align="center">bla</div>
    </body>

    The alert shows 0...



    I was expected that the element should show his offsetLeft relative to it's parent, which is body, but it does not... How the heck can I really find out the position of some element relative to the body? Generally speaking, to retreive the position relative to the body, nomatter the other container parents which that element might have?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Second surprise is that if I use a table instead of a div, the offset seems to work

    <script language="JavaScript" type="text/JavaScript">
    onload=function(){alert(document.getElementById('myid').offsetLeft)}
    </script>
    </head>
    <body>
    <table width="400" border="0" align="center" cellpadding="2" cellspacing="2" id="myid">
    <tr>
    <td>bla</td>
    </tr>
    </table>
    </body>

    That is really weird... hm
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,746
    Thanks
    0
    Thanked 244 Times in 239 Posts
    Hi there kor,

    it works with the table because you have given it a dimension.

    This works with a div...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>offsetLeft function</title>
    
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
    
    <style type="text/css">
    /*<![CDATA[*/
    #myid {
         width:300px;
         margin:auto;
     }
    /*//]]>*/
    </style>
    
    <script type="text/javascript">
    //<![CDATA[
    onload=function() {
         alert(document.getElementById('myid').offsetLeft);
     }
    //]]>
    </script>
    
    </head>
    <body>
    
    <div id="myid"style="">bla</div>
    
    </body>
    </html>
    coothead

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    oh... yea, stupid my oh my (despite Garfield, I hate Wednesdays ). I forgot that I have no width. Even I don't sense why I need the width, as long as the element position is the position of it's left/top corner... Well...

    But a problem still occures... If I have a div inside another dimensioned parentNode, I expect that my div should have 100% of parent without mention that by all means.

    So that to find out the real position of an element, the element and it's parents must have by all means dimensions?.

    I wonder... Width is enough? Or I need the height...But table has the height of it's elements, nomatter the given value...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •