How to use position() Method in JQuery

In this article, I will explain use of position() Method in JQuery.
  • 2452

JQuery CSS position() Method

  • position() method returns the relative position for selected element.

  • position() method returns an object with 2 properties, top and left, which represent the top and left positions in pixels.

Syntax

$(selector).position()

Example

The following example shows how to position() method in jquery.

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function () {

        $("button").click(function () {

            x = $("p").position();

            alert("Left position: " + x.left + "\n Top position: " + x.top);

        });

    });

</script>

</head>

<body>

<p>This is a paragraph position example.</p>

<button>Click for get the position coordinates of the p element</button>

</body>

</html>

 

Output

 

position method.jpg

Further Readings

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.