...

View Full Version : element position



Kor
03-16-2005, 01:10 PM
I am very surprised to see that I can not use offset postion to find the real position of an element:

<script language="JavaScript" type="text/JavaScript">
onload=function(){alert(document.getElementById('myid').offsetLeft)}
</script>
...
<body>
<div id="myid" align="center">bla</div>
</body>

The alert shows 0...

:confused:

I was expected that the element should show his offsetLeft relative to it's parent, which is body, but it does not... How the heck can I really find out the position of some element relative to the body? Generally speaking, to retreive the position relative to the body, nomatter the other container parents which that element might have?

Kor
03-16-2005, 01:17 PM
Second surprise is that if I use a table instead of a div, the offset seems to work

<script language="JavaScript" type="text/JavaScript">
onload=function(){alert(document.getElementById('myid').offsetLeft)}
</script>
</head>
<body>
<table width="400" border="0" align="center" cellpadding="2" cellspacing="2" id="myid">
<tr>
<td>bla</td>
</tr>
</table>
</body>

That is really weird... hm:confused:

coothead
03-16-2005, 01:51 PM
Hi there kor,

it works with the table because you have given it a dimension. :)

This works with a div...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>offsetLeft function</title>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<style type="text/css">
/*<![CDATA[*/
#myid {
width:300px;
margin:auto;
}
/*//]]>*/
</style>

<script type="text/javascript">
//<![CDATA[
onload=function() {
alert(document.getElementById('myid').offsetLeft);
}
//]]>
</script>

</head>
<body>

<div id="myid"style="">bla</div>

</body>
</html>

coothead

Kor
03-16-2005, 02:02 PM
oh... yea, stupid my oh my (despite Garfield, I hate Wednesdays :D ). I forgot that I have no width. Even I don't sense why I need the width, as long as the element position is the position of it's left/top corner... Well...

But a problem still occures... If I have a div inside another dimensioned parentNode, I expect that my div should have 100% of parent without mention that by all means.

So that to find out the real position of an element, the element and it's parents must have by all means dimensions?.

I wonder... Width is enough? Or I need the height...But table has the height of it's elements, nomatter the given value...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum