Peter Johnson
03-07-2007, 06:23 PM
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.
<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>
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.
<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>