1

Is there any way to find the co-ordinates of a right click event on a div element as I have to set context menu based on the position of click.

Any help and suggestion will be appreciated. Thanks.

AKS
  • 45
  • 1
  • 6
  • 1
    It's best to make an attempt, write some code, and return if you're unsure of how to progress/there are errors you don't understand. On principle, SO generally won't write your code for you from scratch – jonny Mar 30 '16 at 11:17
  • x and y from the whole page? than use the clientX and clientY – TEST Mar 30 '16 at 11:18

2 Answers2

4

You can try:

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

And for whole page:

$('div').on('contextmenu', function (e) {
    console.log(e.clientX);
    console.log(e.clientY);
});

Fiddle: https://jsfiddle.net/shree/awf5u2xx/1/

Dolan
  • 1,300
  • 4
  • 15
  • 32
4b0
  • 20,627
  • 30
  • 92
  • 137
2
$(document).ready(function(){ 
  document.oncontextmenu = function() {return false;};

  $(document).on('mousedown', '#TargetElementId', function (e){ 
    if( e.button == 2 ) { // Right mouse button clicked

      return {e.pageX, e.pageY} //return co-ordinates
    } 
    return true; 
  }); 
});

From SO Answer to find right mouse button clicked

Community
  • 1
  • 1
Shb
  • 802
  • 1
  • 14
  • 26