jQuery offsetParent()
The offsetParent()
method in jQuery returns the first positioned parent element of a specified element. This can be useful for finding the offset position of an element relative to its parent element.
Syntax
The syntax for the offsetParent()
method is as follows:
$(selector).offsetParent()
Where selector
is the element we want to locate the offset parent for.
Use
The offsetParent()
method is commonly used for calculating the offset position of a child element relative to its parent. It can be useful in situations where you need to position elements dynamically, such as in a draggable or resizable interface.
Example
Here is an example of using the offsetParent()
method:
<div style="position: relative;">
<div style="position: absolute; top: 10px; left: 10px;">
<p>Hello World!</p>
<p>Offset Parent is: <span id="parent"></span></p>
</div>
</div>
const parent = $('p').offsetParent().get(0).nodeName;
$('#parent').text(parent);