Results 1 to 4 of 4

Thread: one more offsetTop question

  1. #1
    Join Date
    Feb 2007
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default one more offsetTop question

    I've been trying to figure out how to get an element's offsetTop position, when the element is within a table. The script below works fine outside of a table, but returns -1 when it's in a table, or if there are tables anywhere on the page.

    I know (think?) I need to keep climbing the "tree" to get to the top of the page, and have seen scripts such as at http://www.quirksmode.org/js/findpos.html, but can't figure out how to apply them. Could someone show me how this script could be re-written to get the "sectionaposition" when there are tables? I need to use offsetTop, and I need to use it on a site that has tables, so eliminating the tables or using something other than offsetTop aren't options at this point. Thanks.

    Code:
    <html>
    <head>
    </head>
    <body>
    <p>lorum ipsum</p>
    
    <table><tr><td>
    
    
    <div id="sectiona">
    Section A
    </div>
    
    <div id="sectionb">
    <script type="text/javascript">
    var sectionaposition = document.getElementById('sectiona').offsetTop;
    document.write(sectionaposition);
    </script>
    </div>
    
    </td></tr></table>
    
    </body>
    </html>
    Last edited by Peter Johnson; 03-08-2007 at 01:35 AM. Reason: had to close div tag

  2. #2
    Join Date
    Feb 2007
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Here's another version I've tried. This one works in Firefox, Opera, but not IE7

    Code:
    <html>
    <head>
    </head>
    <body>
    <p>lorum ipsum</p>
    
    <table><tr><td>
    
    <div id="sectiona">
    Section A
    </div>
    
    
    <div id="sectionb"> 
    
    <script type="text/javascript">
    var sectionaposition = document.getElementById('sectiona');
    function getRealTop(el) {
        yPos = el.offsetTop;
        tempEl = el.offsetParent;
        while (tempEl != null) {
            yPos += tempEl.offsetTop;
            tempEl = tempEl.offsetParent;
        }
        return yPos;
    }
    trueY = getRealTop(sectionaposition);
    document.write(trueY);
    </script>
    
    </div>
    
    </td></tr></table>
    
    </body>
    </html>

  3. #3
    Join Date
    Feb 2007
    Posts
    293
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    IE7 is buggy about tables. You need to add <tbody> and </tbody> tage.
    Also, the javascript will not work so long as it is in the table. It needs to be moved to the end of the page, just before you close the body tag.

  4. #4
    Join Date
    Jun 2005
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Peter,

    Have had a similar problem - did you find an answer please ?

    Saz

Bookmarks

Posting Permissions

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