2

Well, I'm using this function to get me the X Y coordinates

<input type="image" src="image.jpeg" name="foo" ondblclick="dclick()"  onclick="lclick()" style="cursor:crosshair"  value=""/>

$(document).ready(function() {
  $('image').click(function(e) {
    var offset = $(this).offset();
    alert(e.clientX - offset.left);
    alert(e.clientY - offset.top);
  });
});

My problem is that this (obviously) only work with the left mouse click... how can I adapt it to the right click?

Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
Diogo Leones
  • 147
  • 1
  • 2
  • 9

1 Answers1

4

Try:

    $('.image').on('contextmenu', function (e) {
        console.log(e.pageX),
        console.log(e.pageY);
    });

Context menu is the event for right click. Notice that you need either a '.' or a '#' before the text in the selector, or if you want it to apply to all image tags, then $('img')... If you want to explore what's in an event, just use console.log(e) to have a browse around what you can garner from an event.

David Gilbertson
  • 4,219
  • 1
  • 26
  • 32
  • this will work perfect for a custom context menu, since pageX and pageY return exact mouse position on right click. Thanks – quakeglen Dec 15 '20 at 17:11