I call this function with two elements having the same parentNode
:
var test = function(target, div){
alert(div.parentNode == target.parentNode ); // gives true
alert(div.offsetParent == target.offsetParent); // gives true
div.setAttribute("style", "position:relative; float:left; height:" + target.offsetHeight + "px;");
alert(div.parentNode == target.parentNode); // gives true
alert(div.offsetParent == target.offsetParent); // gives false!!!
}
As you can see that both elements have same parent, which means they are inside same branch of the DOM tree, how e they have different offsetParent
?
I notice here that div has relative position, which seems to be the reason because when I remove it both alerts give true. (but normally the position of an element should not affect its offsetParent
)
/* I have edited the function after some more invistigation it should show more where lies the problem */ I got the same results on FF and Chrome. parentNode of both elements is a table-td
Thank you for answering.
I call this function with two elements having the same parentNode
:
var test = function(target, div){
alert(div.parentNode == target.parentNode ); // gives true
alert(div.offsetParent == target.offsetParent); // gives true
div.setAttribute("style", "position:relative; float:left; height:" + target.offsetHeight + "px;");
alert(div.parentNode == target.parentNode); // gives true
alert(div.offsetParent == target.offsetParent); // gives false!!!
}
As you can see that both elements have same parent, which means they are inside same branch of the DOM tree, how e they have different offsetParent
?
I notice here that div has relative position, which seems to be the reason because when I remove it both alerts give true. (but normally the position of an element should not affect its offsetParent
)
/* I have edited the function after some more invistigation it should show more where lies the problem */ I got the same results on FF and Chrome. parentNode of both elements is a table-td
Thank you for answering.
.offsetParent
and .parentNode
will not necessary give the same value. As they are getting difference items depending on the structure of the document.
– Spencer Wieczorek
Commented
Jul 29, 2016 at 6:24
Just because both properties contain the word "parent" does not mean they are similar.
offsetParent
property returns the closest element in the ancestors hierarchy that is positioned.parentNode
property simply returns the immediate parent element.Now, if two elements are siblings (as in your example) then their offset parents should be same. However, it is possible for an element to return null
instead of offset parent, for example:
display: none
position: fixed
Edit: your example involves a td
element so the offset parent is calculated in a weird manner. Inside a td
element:
td
element as their offset parent. This means target.offsetParent
will be that td
.div.offsetParent
will be body
since div is positioned.The simplest workaround to make both elements have same offset parent is to add position: relative
on the table cell.
offsetParent is the closest parent that has position, so when the div has position itself it is its own offsetParent.