Search code examples
javascriptjqueryright-click

Using jQuery how to get right click coordinates on the target element


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.


Solution

  • 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/