Skip to content
Related Articles

Related Articles

Improve Article
How to validate position in jQuery ?
  • Last Updated : 23 Apr, 2021

In this article, we will find how to validate position using jQuery position() method. The .position() method in jQuery returns the position of the first matched element relative to its parent element.

Syntax:

$(selector).position()

Return value:

  • Returns an object containing the properties top and left.

Example: Let us take an example to understand .position() method of jQuery.

HTML




<!DOCTYPE HTML>
<html>
<head>
    <script src=
    </script>
</head>
<body style="text-align:center;">
    <h2 id="heading" style="color:green;">
        GeeksforGeeks
    </h2>
    <p>
        jQuery | .position() method
    </p>
  
    <button id="button">
        click here to get position of heading
    </button>
        
    <script>
    // Selecting h2 tag and using .positon() method 
    // to find its position relative to offset parent.
     $(document).ready(function(){
      $("button").click(function(){
        var x = $("h2").position();
        $( "p" ).last().text ("Top position is : " + 
               x.top + " Left position is : " + x.left);
      });
    });
    </script>
</body>  
</html>

Output :

jQuery position method

My Personal Notes arrow_drop_up
Recommended Articles
Page :