Open In App

jQuery position() Method

Last Updated : 10 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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 () {
                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: 
jquery-20
Related Articles: 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads