Skip to content
Related Articles

Related Articles

jQuery | position() with Examples

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 30 Aug, 2021

The position() method is an inbuilt method in jQuery which is used to find the position of the first matched element relative to its parent element in the DOM tree.
 

Syntax:  

$(selector).position()

Parameters: This method does not contains any parameters.
Return Value: This method returns the position of the first matched element.
Below example illustrate the position() method in jQuery:
Example: 

html




<!DOCTYPE html>
<html>
   <head>
       <title>The position Method</title>
      <script src=
      </script>
       
      <!-- jQuery code to show the working of this method -->
      <script>
         $(document).ready(function(){
             $("button").click(function(){
                 var x = $("p").position();
                 alert("Top position: " + x.top);
             });
         });
      </script>
      <style>
         div {
             width: 350px;
             height: 100px;
             font-weight: bold;
             padding:20px;
             font-size: 25px;
             border: 2px solid green;
         }
      </style>
   </head>
   <body>
      <div>
          
<p>GeeksforGeeks.</p>
 
          
         <!-- Click on this button  -->
         <button>Click Here!</button>
      </div>
   </body>
</html>

Output: 
Before click on the button: 
 

After click on the button: 
 

Related Articles: 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!