473,409 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Firefox DOM -- offsetParent vs parentNode

Anyone know what the specific difference is between the offsetParent
and parentNode properties of a DOM element are?

Mozilla's docs on the DOM element
(http://developer.mozilla.org/en/docs/DOM:element) don't really
indicate much of a difference, but whatever the difference is is really
screwing me up in one of my projects. On a specific div node nested
within a whole bunch of other div nodes in the DOM, the parentNode
property evaluates to the actual parent node, whereas offsetParent
evaluates to null. This doesn't really affect me, but when I go to
clone the node to append it to clone's real parent for another purpose,
the parentNode of the new clone node remains the actual parent I
appended it to whereas the offsetParent becomes the HTML body element!
(Also, just so you know, I checked the parentNode / offsetParent before
I cloned it to make sure it wasn't a problem with the clone procedure.)

The reason I need the offsetParent instead of the parentNode is because
offsetTop for each of my new cloned nodes is screwed up. Because
offsetParent is the body node, offsetTop gives me the pixel count
between the top of the browser and the element itself. I need the
offsetTop of the container element, the _real_ parent node.

There's nothing really special about this node as far as I can tell.
I'm using FF 1.5.0.7 and a XHTML 1.0 transitional doctype. I haven't
tested any other elements on the DOM for this weird behaviour, and I
haven't tried it in other browsers yet. I've done things similar to
this before though, and I've never had problems with it. Any ideas of
how I can fix this problem, or where I can look for more info? Thanks.

Andrew

Oct 27 '06 #1
2 15786
am*****@gmail.com wrote:
Anyone know what the specific difference is between the offsetParent
and parentNode properties of a DOM element are?
The offsetParent property refers to the nearest positioned (non-static)
ancestor element. If there is no such element, offsetParent will refer
to the body element[1].

Consider the following fragment:

<body>
<div>
<p>Lorum ipsum...</p>
</div>

<div style="position: relative;">
<p>Lorum ipsum...</p>
</div>
</body>

The offsetParent property of the first paragraph will refer to the body
element. There are no positioned ancestor elements, so that default is
used. In the second case, the offsetParent property will refer to the
div element as that is positioned. Here, the div element is the parent,
but it doesn't have to be.
Mozilla's docs on the DOM element
(http://developer.mozilla.org/en/docs/DOM:element) don't really
indicate much of a difference ...
That particular document doesn't, but the actual description of the
property[2] does.
On a specific div node nested within a whole bunch of other div nodes
in the DOM, the parentNode property evaluates to the actual parent
node, whereas offsetParent evaluates to null.
I can't say I've seen that before. Can you post a link to a small
demonstration?
This doesn't really affect me, but when I go to clone the node to
append it to clone's real parent for another purpose, the parentNode
of the new clone node remains the actual parent I appended it to
whereas the offsetParent becomes the HTML body element!
Unless there is a positioned ancestor, that should be expected.

[snip]
I'm using FF 1.5.0.7 and a XHTML 1.0 transitional doctype.
<aside>
Serving XHTML to clients is typically pointless. Doubly so if the
Transitional document type is used. The latter should be avoided (if at
all possible, and it usually is) even for HTML.
</>

[snip]

Mike
[1] The Mozilla documentation indicates that it should be the
root element, but this doesn't seem to be the case in
practice most likely because MSIE uses the body element.
[2] <http://developer.mozilla.org/en/docs/DOM:element.offsetParent>
Oct 27 '06 #2
Well, that solved my problem. I actually had read the wiki pages for
those properties, but for whatever reason I just ignored the whole
"relative positioned parent" thing and hadn't even tried what you
suggested before I posted. I don't remember having to set position:
relative when I did that before, but I guess I must have.

Also, after trying that, I found out that my problem with having a null
offsetParent and the "real" parentNode was that the parent's parent had
display: none set on it. I removed the display: none later in the
script before I started cloning nodes and all, but I was erraneously
eval'ing the parentNode / offsetParent before I set display: block on
the container element. Therefore, the offsetParent was null and the
parentNode was the "real" node at that time.

Anyway, thanks for the help!

Oct 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Keith Thornhill | last post by:
I'm trying to calculate the top and left distance from the side of the browser of an object (either absolutely or relatively positioned) and i'm running into a weird problem as i'm trying to make...
3
by: ara.pehlivanian | last post by:
I've got a script that determines the true position of an element in the page by cycling up the .offsetParent elements all the way up to the <body> element and tallying up the .offsetLeft values...
2
by: Mark Szlazak | last post by:
The following code fails in Firefox to get at selected text in the right-side textarea. Any help would be appreciated. <html> <head> <script> var agt = navigator.userAgent.toLowerCase();...
1
by: Mickey | last post by:
Hi, I have a script which works in both IE but is sluggish in Firefox. The script is a simple text scroller. It works perfectly in IE however in Firefox, the text scroll slower and eventually...
5
by: webEater | last post by:
In XHTML mode (application/xhtml+xml) Firefox does not know offset properties (offsetParent/Left/Top). In all the other browsers it works. Does someone has the same problem or knows a solution for...
1
by: inteli | last post by:
Hey anybody can help me out wid this code..Its working well in IE but nt in firefox.The problem lies in offset,clientX,clientY part but I dnt know how to resolve it .. var zxcDockAry=new Array();...
2
by: Geoffrey Summerhayes | last post by:
I'm trying to select a row from a table in a 'dialog box' where the HTML is (boiled down): <body> <div style='position:absolute; display:block;...'> ...other divs... <div...
10
by: Humakt | last post by:
Hey all. It's been a while since I last visited. Short description of program: Board game which's html elements are fully constructed using DHTML/Javascript. Board is constructed of html table,...
1
by: sva0008 | last post by:
i have a auto suggest script that does not work in firefox , works great on IE. /******************************************************* AutoSuggest - a javascript automatic text input...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.