View Full Version : bug in calculating the position of an element

07-08-2004, 03:29 PM

I wrote a function which will find the exact top left coordinates in the screen.
The function will return an associative array with information on the element:

function getPositionAndDimension(element) {
var left = 0;
var top = 0;
var width = element.offsetWidth;
var height = element.offsetHeight;
while (element.parentElement != null) {
left += element.offsetLeft;
top += element.offsetTop;
element = element.parentElement;

var results = new Array(4);
results['left'] = left;
results['top'] = top;
results['width'] = width;
results['height'] = height;

return results;

The problem is that the value that the function calculates for the element's top is incorrect.
I am getting values that are much larger than the actual top value.

can anyone see where the bug is in?
I am testing this on IE6


07-08-2004, 07:00 PM
Have you tried offsetParent instead of parentElement?

07-08-2004, 09:56 PM
yes, that did the trick.