jQuery | offsetParent() with Example
The offsetParent() method is an inbuilt method in jQuery which is used to find the first positioned parent element in the DOM tree.
Syntax:
$(selector).offsetParent()
Parameters: This method does not contains any parameter.
Return Value: This method returns the first positioned parent element.
Below example illustrates the offsetParent() method in jQuery:
Example:
<!DOCTYPE html> <html> <head> <title>The offsetParent Method</title> <script src= </script> <!-- jQuery code to show the woirking of this method --> <script> $(document).ready(function(){ $("button").click(function(){ $("p").offsetParent().css("background-color", "green"); }); }); </script> <style> #d1 { border:1px solid black; width:70%; position:absolute; left:10px; top:50px } #d2 { border:1px solid black; margin:50px; background-color:lightblue; text-align:center; } p { padding-left: 80px; font-weight: bold; } </style> </head> <body> <button>Submit</button> <div id="d1"> <div id="d2"> <!-- click on this paragraph --> <p>Welcome to GeeksforGeeks!</p> </div> </div> </body> </html> |
chevron_right
filter_none
Output:

Related Articles:
Recommended Posts:
- jQuery | jQuery.support Property
- jQuery | jQuery.fx.interval Property with example
- jQuery | jquery Property
- jQuery | jQuery.fx.off Property
- jQuery | css()
- jQuery | :even Selector
- jQuery | last() with Examples
- jQuery | die() Method
- jQuery | focusout() with Example
- jQuery | on() with Examples
- jQuery | :first Selector
- jQuery | #id Selector
- jQuery | parentsUntil() with Example
- jQuery | after() with Examples
- jQuery | :contains() Selector
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



