Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

jQuery | offsetParent() with Example

  • Last Updated : 30 Aug, 2021

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: 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

html




<!DOCTYPE html>
<html>
   <head>
       <title>The offsetParent Method</title>
      <script src=
      </script>
       
      <!-- jQuery code to show the working 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>

Output: 
 

offsetparent

Related Articles: 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!