.offsetParent()
JQ Home <<
Traversal <<
.offsetParent()
First positioned ancestor element retrieval.
Description
The .offsetParent()
method is used to retrieve the closest positioned ancestor element.
- If an element has a CSS position attribute of
absolute
,relative
orfixed
it is a positioned element. - This information is useful for calculating offsets for performing effects and placing elements on the page.
Syntax
Signature | Description |
---|---|
.offsetParent() | Retrieve the descendants of each element within the matched set that matches the specified selector. |
Parameters
None.
Return
A jQuery
object either containing the element matched or empty.
.offsetParent()
Example
Traversal << Top
Retrieve the closest positioned ancestor element.
In the example below we get the closest positioned ancestor element of the list item with an id of 'list2', (the position relative list item below) and turn the background colour to yellow.
- Things To Do When I Get Up
- Put The Kettle On
- Put The Teabag in The Cup
- Wait for Kettle To Boil
- Pour Boiling Water In The Cup (position relative)
- Wait for tea to brew
- Remove teabag
- Pour in milk
- Wait for tea to brew
- Remove teabag (id of list1)
- Things To Do When I Go To Bed
$(function(){
$('#btn46').on('click', function() {
$('#list1').offsetParent()
.css('backgroundColor', 'yellow');
});
});
Related Tutorials
jQuery Basic Tutorials - Lesson 6 - Tree Traversal