How to get relative click coordinates on the target element using JQuery?
Here is the code to get the coordinates of the element, Here 2 methods are discussed one is calculating the relative position to its parent while another is calculating the relative position to the document.
Approach 1:
- Attach the click event to the element.
- Call an anonymous function when an event occurs.
- Calculate the X relative to the parent element by subtracting the Offset().left property from the pageX property.
- Similarly, calculate the Y relative to parent element by subtracting the Offset().top property from the pageY property.
Example 1: This example follows the approach discussed above to calculate the location’s relative position to its parent element.
<!DOCTYPE HTML> <html> <head> <title> How to get relative click coordinates on the target element using JQuery? </title> <script src= </script> <style> h1 { border: 1px solid green; } #GFG_UP { border: 1px solid green; } button { border: 1px solid green; } </style> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var x, y; el_up.innerHTML = "Click inside any bordered element to get the"+ "click coordinates of that particular location with respect"+ "to its parent element "; $('h1').click(function(e) { // element that has been clicked. var elm = $(this); // getting the respective x = e.pageX - elm.offset().left; // coordinates of location. y = e.pageY - elm.offset().top; gfg_Run(); }); $('#GFG_UP').click(function(e) { var elm = $(this); // getting the respective x = e.pageX - elm.offset().left; // coordinates of location. y = e.pageY - elm.offset().top; gfg_Run(); }); function gfg_Run() { el_down.innerHTML = "X- " + x + "<br>Y- " + y; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Approach 2:
- Attach the click event to the element.
- Call an anonymous function when event occurs.
- Calculate the X relative to document by pageX property.
- Similarly, calculate the Y relative to document by pageY property.
Example 2: This example follows the approach discussed above to calculate the location’s relative position to the document.
<!DOCTYPE HTML> <html> <head> <title> How to get relative click coordinates on the target element using JQuery? </title> <script src= </script> <style> h1 { border: 1px solid green; } #GFG_UP { border: 1px solid green; } button { border: 1px solid green; } </style> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var x, y; el_up.innerHTML = "Click inside any bordered element "+ "to get the click coordinates of that particular "+ "location with respect to document"; $('h1').click(function(e) { // element that has been clicked. var elm = $(this); // getting the respective x = e.pageX; // coordinates of location. y = e.pageY; gfg_Run(); }); $('#GFG_UP').click(function(e) { // element that has been clicked. var elm = $(this); // getting the respective x = e.pageX; // coordinates of location. y = e.pageY; gfg_Run(); }); function gfg_Run() { el_down.innerHTML = "X- " + x + "<br>Y- " + y; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to get the coordinates of a mouse click on a canvas element ?
- How to perform click-and-hold operation inside an element using jQuery ?
- jQuery | click() with Examples
- How to distinguish left and right mouse click using jQuery?
- How to disable Copy, Paste, Cut and Right Click using jQuery ?
- jQuery | event.target Property with Example
- How to remove parent element except its child element using jQuery ?
- jQuery | Move an element into another element
- How to get a DOM Element from a jQuery Selector ?
- How to create a div element in jQuery ?
- How to get focused element using jQuery?
- jQuery | element Selector
- jQuery | element + next Selector
- How to select an element by name with jQuery?
- jQuery | Create a div element
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.

