In order to get the location of an element relative to the document, jQuery offset() method is used. The offset() method is an inbuilt method in jQuery which is used to set or returns the offset coordinates of the selected element. We can also use the jQuery position() method. The position() method is an inbuilt method in jQuery which is used to find the position of the first matched element relative to its parent element in the DOM tree.
Syntax:
$(selector).offset()
Below examples illustrate the above approach:
Example 1:
<!DOCTYPE html> <html> <head> <title> How to get the Location of an element relative to the document using jQuery? </title> <style> h1 { color: green; } body { text-align: center; } </style> <script src= </script> </head> <body> <h1>GeeksforGeeks</h1> <b> How to get the Location of an element relative<br> to the document using jQuery/JavaScript? </b> <br><br> <div id="Logo"> <img src= </div> <br><br> <button type="button">Click</button> <h4></h4> <script> $(document).ready(function() { $("button").click(function() { var offset = $("#Logo").offset(); $("h4").text("Location of the box is: (left: " + offset.left + ", top: " + offset.top + ")"); }); }); </script> </body> </html> |
Output:

Example 2:
<!DOCTYPE html> <html> <head> <title> How to get the Location of an element relative to the document using jQuery? </title> <style> h1 { color: green; } body { text-align: center; } </style> <script src= </script> </head> <body> <h1>GeeksforGeeks</h1> <b> How to get the Location of an element relative<br> to the document using jQuery/JavaScript? </b> <br><br> <div id="Logo"> <img src= </div> <br><br> <button type="button">Click</button> <h4></h4> <script> $(document).ready(function() { $("button").click(function(){ var position = $("#Logo").position(); $("h4").text("Location of the Logo is: (left: " + position.left + ", top: " + position.top + ")"); }); }); </script> </body> </html> |
Output:

Recommended Posts:
- How to get relative click coordinates on the target element using JQuery?
- How to remove parent element except its child element using jQuery ?
- JQuery | Get the n-th level parent of an element
- How to position relative to top of a text canvas using Fabric.js ?
- How to set position relative to top of a canvas circle using Fabric.js ?
- How to set position relative to left of a canvas circle using Fabric.js ?
- How to get the child element of a parent using JavaScript ?
- How to get the number of pages in a PDF document using PHP ?
- Get the relative timestamp difference between dates in JavaScript
- How to get the entire HTML document as a string in JavaScript ?
- How to find the parent class name with known class in jQuery ?
- jQuery | parent() & parents() with Examples
- jQuery | parent > child Selector
- jQuery | :parent Selector
- jQuery | parent descendant Selector
- How to Check if an element is a child of a parent using JavaScript?
- Convert relative path URL to absolute path URL using JavaScript
- How to convert an HTML element or document into image ?
- How to get focused element using jQuery?
- How to get N options from the <select> element using JQuery?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

