103

I would like to use jQuery to get the X/Y coordinates of a click event on an image. The coordinates should be relative to the image, not relative to the whole page

Ron Harlev
  • 16,227
  • 24
  • 89
  • 132
  • 1
    These 3 links provide good answers http://stackoverflow.com/a/4249711/ http://stackoverflow.com/a/3236129/ http://stackoverflow.com/a/10429969/ – user Aug 01 '14 at 07:13

5 Answers5

178

You can use pageX and pageY to get the position of the mouse in the window. You can also use jQuery's offset to get the position of an element.

So, it should be pageX - offset.left for how far from the left of the image and pageY - offset.top for how far from the top of the image.

Here is an example:

$(document).ready(function() {
  $('img').click(function(e) {
    var offset = $(this).offset();
    alert(e.pageX - offset.left);
    alert(e.pageY - offset.top);
  });
});

I've made a live example here and here is the source.

To calculate how far from the bottom or right, you would have to use jQuery's width and height methods.

erjiang
  • 44,417
  • 10
  • 64
  • 100
Brian McKenna
  • 45,528
  • 6
  • 61
  • 60
  • @Brian : Thanks so much for your solution!!! This resolves my problem when webpage zooms in/out. – Michael Mao Mar 12 '10 at 07:01
  • 13
    still doesn't work hen you scroll the page it spits different results – beerwin Jan 11 '12 at 10:01
  • This was exactly what I was looking for, but took me a while to put into words what I was looking for. – Josh May 14 '12 at 21:08
  • 19
    As @beerwin mentioned, this doesn't work when scrolling the page. If you need the coordinates based on the page rather than the viewable window, use `e.pageX` and `e.pageY` instead. – Josh May 15 '12 at 18:37
  • 1
    Exactly! In most cases e.pageX and e.pageY is what you want. – NetWave Jun 11 '14 at 14:37
  • Sorry @BrianMcKenna, I don't want to get the position, I want direct click in the position of the tag. How to do this? – Ave Jul 29 '16 at 06:07
  • @vanloc did you find solution on your click? – Ritesh katare Oct 07 '20 at 12:23
26

note! there is a difference between e.clientX & e.clientY and e.pageX and e.pageY

try them both out and make sure you are using the proper one. clientX and clientY change based on scrolling position

axel22
  • 32,045
  • 9
  • 125
  • 137
Adam
  • 261
  • 3
  • 2
20

Here is a better script:

$('#mainimage').click(function(e)
{   
    var offset_t = $(this).offset().top - $(window).scrollTop();
    var offset_l = $(this).offset().left - $(window).scrollLeft();

    var left = Math.round( (e.clientX - offset_l) );
    var top = Math.round( (e.clientY - offset_t) );

    alert("Left: " + left + " Top: " + top);

});
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
rckehoe
  • 1,198
  • 15
  • 16
9

The below code works always even if any image makes the window scroll.

$(function() {
    $("#demo-box").click(function(e) {

      var offset = $(this).offset();
      var relativeX = (e.pageX - offset.left);
      var relativeY = (e.pageY - offset.top);

      alert("X: " + relativeX + "  Y: " + relativeY);

    });
});

Ref: http://css-tricks.com/snippets/jquery/get-x-y-mouse-coordinates/

itsazzad
  • 6,868
  • 7
  • 69
  • 89
4

Take a look at http://jsfiddle.net/TroyAlford/ZZEk8/ for a working example of the below:

<img id='myImg' src='/my/img/link.gif' />

<script type="text/javascript">
    $(document).bind('click', function () {
        // Add a click-handler to the image.
        $('#myImg').bind('click', function (ev) {
            var $img = $(ev.target);

            var offset = $img.offset();
            var x = ev.clientX - offset.left;
            var y = ev.clientY - offset.top;

            alert('clicked at x: ' + x + ', y: ' + y);
        });
    });
</script>

Note that the above will get you the x and the y relative to the image's box - but will not correctly take into account margin, border and padding. These elements aren't actually part of the image, in your case - but they might be part of the element that you would want to take into account.

In this case, you should also use $div.outerWidth(true) - $div.width() and $div.outerHeight(true) - $div.height() to calculate the amount of margin / border / etc.

Your new code might look more like:

<img id='myImg' src='/my/img/link.gif' />

<script type="text/javascript">
    $(document).bind('click', function () {
        // Add a click-handler to the image.
        $('#myImg').bind('click', function (ev) {
            var $img = $(ev.target);

            var offset = $img.offset(); // Offset from the corner of the page.
            var xMargin = ($img.outerWidth() - $img.width()) / 2;
            var yMargin = ($img.outerHeight() - $img.height()) / 2;
            // Note that the above calculations assume your left margin is 
            // equal to your right margin, top to bottom, etc. and the same 
            // for borders.

            var x = (ev.clientX + xMargin) - offset.left;
            var y = (ev.clientY + yMargin) - offset.top;

            alert('clicked at x: ' + x + ', y: ' + y);
        });
    });
</script>
Troy Alford
  • 26,660
  • 10
  • 64
  • 82
  • My solution, also, needs to be updated to reflect pageX and pageY - just noted the above comments. This will not work correctly when scrolling - but there is already an accepted answer. :) – Troy Alford Jun 20 '12 at 22:54