The jQuery position() method can be used to get the position of the first matched element. It finds the position of the element from the top and left corner of the screen.
Syntax
The syntax of this method is given below:-
The above syntax contains no parameters to use the method.
Example of jQuery position() Method to Get the Position
If you want to find the position of the selected element, you have to use the jQuery position() method. It returns the value of the coordinates from the left side and the top side. You have to specify the position as given in the example below.
Example
1 2 3 4 5 6 7 8 9 10 |
<script> $(document).ready(function(){ $('.mygetbtn').click(function(){ var myoffset = $(".mypara").position(); alert("Left coordinate is: "+myoffset.left+", Top coordinate is:"+myoffset.top); }); }); </script> <button type="button" class="mygetbtn">Click to Return Position</button><br><br> <p class="mypara">This is my paragraph.</p> |
Output
This is my paragraph.
When you click the button given above, it displays the position coordinates of the selected element. Check the alert message showing the values of the position of the element.
You may also like to read
I hope you like this tutorial on jQuery position() method. If you have any queries regarding the tutorial, please comment below.
References