X, Y position problem

  • Thread starter Thread starter Shahar
  • Start date Start date
S

Shahar

Hi

I have some div in a td that in some table, something like that:

<table>
<tr>
<td>
<div id="div">some text</div>
</td>
</tr>
</table>

I need to get the x,y position of the div from a script like that:
<script>
alert(div.style.left);
</script>

but I get an empty string, I can get the x, y only if I write it from
advance, like that:
<div id="div" style="left:50;">some text</div>

is there anyway to get the x,y without setting it from advance ?

Thanx.
 
thats because style.left returns the text attribute of the style if set. you
want the offsetLeft. this is the offset from the objects parent. this
function will give the absolute left

findPosX(obj) {
var curleft = 0;
if (document.getElementById || document.all) {
while (obj.offsetParent) {
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (document.layers)
curleft += obj.x;
return curleft;
}
 
Back
Top