Open In App

jQuery position() Method

The position() method is an inbuilt method in jQuery that 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 contain any parameters.

Return Value: This method returns the position of the first matched element.



Example:  In this example, we are using the jQuery position() method.




<!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 () {
                let x = $("p").position();
                alert("Top position: " + x.top);
            });
        });
    </script>
    <style>
        body {
            display: flex;
            justify-content: center;
        }
 
        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: 

Related Articles: 


Article Tags :