/    Sign up×
Community /Pin to ProfileBookmark

element position

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…

?

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?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@KorauthorMar 16.2005 — 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?
Copy linkTweet thisAlerts:
@KorauthorMar 16.2005 — yeap... I know how to use offsetParent (or parentNode) but I am stuck... If the offsetParent/parentNode is a div like in my example above, there is no use, as its offsetLeft returns 0, instead of some value.

Weird enough is that the TABLE elements seems to have a normal behaviour (I can use offset values) but the DIV tag must be CSS positioned somehow to be able to reterive it's offset. But if I position it using CSS I don't need the offset, I might get the CSS attributes directly...

The problem that I encounter is that I must do something on a page which is centered, thus I don't know the position of the container...
Copy linkTweet thisAlerts:
@baconbuttyMar 16.2005 — The explanations may lie in the pages on this site:-

[URL]http://webreference.com/dhtml/diner/[/URL]

Scroll down to view the element page coordinates pages.

In any case, why should the offsetLeft of a non-positioned div as child of BODY not be 0? What were you expecting, which browser, and standards or quirks mode?
Copy linkTweet thisAlerts:
@KorauthorMar 16.2005 — I've notice later that if I give the element (div or whichever) a [b]width[/b] things turn to normal... Even so, it is a little bizzare that I must set the width of the element even I don't wanna find out more than it's left/top corner position...?
Copy linkTweet thisAlerts:
@baconbuttyMar 16.2005 — It could be something to do with whether the element's display is set to "block".

Perhaps setting width also implicitly sets display to block.
×

Success!

Help @Kor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...