<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Get X/Y Offsets for an Object in JavaScript</title>
	<atom:link href="http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html</link>
	<description>A blog about Django, JavaScript, CSS, and general web development.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:43:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Bim</title>
		<link>http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html/comment-page-1#comment-22004</link>
		<dc:creator>Bim</dc:creator>
		<pubDate>Wed, 27 Jan 2010 13:16:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html#comment-22004</guid>
		<description>^^

Don&#039;t know how many years old this is now.

I&#039;m in the same position you were right now, i keep running into problems with  objects with javascript and how to get their x and y values.

Now i&#039;m beginning to think theres some basic javascript thing to do with form elements that makes them different to other objects and theres some other way you have to get to them properties.  I&#039;m gonna skip using it for now but this comes up for me quite often if someone knows how to access form element properties it would be good!</description>
		<content:encoded><![CDATA[<p>^^</p>
<p>Don&#8217;t know how many years old this is now.</p>
<p>I&#8217;m in the same position you were right now, i keep running into problems with  objects with javascript and how to get their x and y values.</p>
<p>Now i&#8217;m beginning to think theres some basic javascript thing to do with form elements that makes them different to other objects and theres some other way you have to get to them properties.  I&#8217;m gonna skip using it for now but this comes up for me quite often if someone knows how to access form element properties it would be good!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gabe da silveira</title>
		<link>http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html/comment-page-1#comment-21203</link>
		<dc:creator>gabe da silveira</dc:creator>
		<pubDate>Wed, 30 Dec 2009 19:51:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html#comment-21203</guid>
		<description>Please add the year to your post dates.  It&#039;s a crime against information.</description>
		<content:encoded><![CDATA[<p>Please add the year to your post dates.  It&#8217;s a crime against information.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jamesgayo</title>
		<link>http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html/comment-page-1#comment-21034</link>
		<dc:creator>jamesgayo</dc:creator>
		<pubDate>Fri, 04 Dec 2009 05:34:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html#comment-21034</guid>
		<description>oh sorry... i got it now. thanks again.</description>
		<content:encoded><![CDATA[<p>oh sorry&#8230; i got it now. thanks again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jamesgayo</title>
		<link>http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html/comment-page-1#comment-21033</link>
		<dc:creator>jamesgayo</dc:creator>
		<pubDate>Fri, 04 Dec 2009 02:51:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html#comment-21033</guid>
		<description>please how do you get firefox to return a value from this:
document.defaultView.getComputedStyle(elem,&#039;&#039;).getPropertyValue(&#039;border-left-width&#039;)  ? It&#039;s always nothing :(
thanks a lot</description>
		<content:encoded><![CDATA[<p>please how do you get firefox to return a value from this:<br />
document.defaultView.getComputedStyle(elem,&#8221;).getPropertyValue(&#8216;border-left-width&#8217;)  ? It&#8217;s always nothing <img src='http://www.davidcramer.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /><br />
thanks a lot</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Honza Odvárko</title>
		<link>http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html/comment-page-1#comment-20275</link>
		<dc:creator>Honza Odvárko</dc:creator>
		<pubDate>Tue, 16 Sep 2008 12:40:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html#comment-20275</guid>
		<description>Thank you for a very helpful solution. I just don&#039;t undesrtand, why you initialize curtop to this value:

var curtop = obj.offsetHeight + 5;</description>
		<content:encoded><![CDATA[<p>Thank you for a very helpful solution. I just don&#8217;t undesrtand, why you initialize curtop to this value:</p>
<p>var curtop = obj.offsetHeight + 5;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anthony</title>
		<link>http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html/comment-page-1#comment-19849</link>
		<dc:creator>anthony</dc:creator>
		<pubDate>Wed, 23 Apr 2008 05:35:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html#comment-19849</guid>
		<description>Thank you! I thought I was going crazy when the &quot;standard&quot; X/Y javascript functions simply didn&#039;t work.

There seems to be a bit of debug code left in your sample above, below is a slightly modified version that works for me.

I&#039;ve also added support for object padding, which also causes the co-ordinates to be off.

Finally, i found that an object may not be set to position=relative, so I changed the logic to be != absolute.

/**
 * Returns the absolute X and Y positions of an object.
 * @param {HTMLObject} obj HTML Object.
 * @return {Object} Returns an accessor with .x and .y values.
 */
function getXY(obj)
{
  var curleft = 0;
  var curtop = 0;
  var border;
  var padding;
  if (obj.offsetParent)
  {
    do
    {
      // XXX: If the element is position: relative we have to add borderWidth
      if (getStyle(obj, &#039;position&#039;) != &#039;absolute&#039;)
      {
        if (border = getStyle(obj, &#039;border-top-width&#039;)) curtop += parseInt(border);
        if (border = getStyle(obj, &#039;border-left-width&#039;)) curleft += parseInt(border);

        if (padding = getStyle(obj, &#039;padding-top&#039;)) curtop += parseInt(padding);
        if (padding = getStyle(obj, &#039;padding-left&#039;)) curleft += parseInt(padding);
      }
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
    while (obj = obj.offsetParent)
  }
  else if (obj.x)
  {
    curleft += obj.x;
    curtop += obj.y;
  }
  return {&#039;x&#039;: curleft, &#039;y&#039;: curtop};
}</description>
		<content:encoded><![CDATA[<p>Thank you! I thought I was going crazy when the &#8220;standard&#8221; X/Y javascript functions simply didn&#8217;t work.</p>
<p>There seems to be a bit of debug code left in your sample above, below is a slightly modified version that works for me.</p>
<p>I&#8217;ve also added support for object padding, which also causes the co-ordinates to be off.</p>
<p>Finally, i found that an object may not be set to position=relative, so I changed the logic to be != absolute.</p>
<p>/**<br />
 * Returns the absolute X and Y positions of an object.<br />
 * @param {HTMLObject} obj HTML Object.<br />
 * @return {Object} Returns an accessor with .x and .y values.<br />
 */<br />
function getXY(obj)<br />
{<br />
  var curleft = 0;<br />
  var curtop = 0;<br />
  var border;<br />
  var padding;<br />
  if (obj.offsetParent)<br />
  {<br />
    do<br />
    {<br />
      // XXX: If the element is position: relative we have to add borderWidth<br />
      if (getStyle(obj, &#8216;position&#8217;) != &#8216;absolute&#8217;)<br />
      {<br />
        if (border = getStyle(obj, &#8216;border-top-width&#8217;)) curtop += parseInt(border);<br />
        if (border = getStyle(obj, &#8216;border-left-width&#8217;)) curleft += parseInt(border);</p>
<p>        if (padding = getStyle(obj, &#8216;padding-top&#8217;)) curtop += parseInt(padding);<br />
        if (padding = getStyle(obj, &#8216;padding-left&#8217;)) curleft += parseInt(padding);<br />
      }<br />
      curleft += obj.offsetLeft;<br />
      curtop += obj.offsetTop;<br />
    }<br />
    while (obj = obj.offsetParent)<br />
  }<br />
  else if (obj.x)<br />
  {<br />
    curleft += obj.x;<br />
    curtop += obj.y;<br />
  }<br />
  return {&#8216;x&#8217;: curleft, &#8216;y&#8217;: curtop};<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
