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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top