Failing to identify right click event in Mozilla Firefox

I'm trying to add some behavior for the right click event. Weirdly I simply cannot make the event handling work.

This is the jQuery code:

$('body').on('click', '#wrapper', null, function(ev){
    if (ev.which === 2 || ev.which === 3){
        alert("Here!");
    }
});

I think that the event is fired but it cannot be identified as a "rightclick". The alert message is never shown. What am I doing wrong? Thanks you!

LE: I am able to identify the left click event with ev.which === 1. So there is no problem there.

Answers:

Answer

instead of click use mousedown:

$('body').on('mousedown', '#wrapper', null, function(ev){
    if (ev.which === 2 || ev.which === 3){
        alert("Here!");
    }
});?
Answer

Using javascript you can try this

document.onmousedown = ListenMouseClick
document.onmouseup = ListenMouseClick

function ListenMouseClick() 
{
if (window.event.button==2 )
    {
        alert('right click')
    }
}
Answer

Easy using ternary operators and 'mousedown' or 'contextmenu'

http://jsfiddle.net/49Ldy/

$('body').on('mousedown','#wrapper',function(ev){
   var evt = ev.which === 3 ? alert('rightClick') : alert('someOtherButton');
});

or:

$('body').on('mousedown','#wrapper',function(ev){
    var evt = ev.which === 3 ? alert('rightClick') : (ev.which === 2 ? alert('auxClick') : alert('someOtherButton'));
});

or:

$('body').on('contextmenu','#wrapper',function(){
    alert('rightClick');
    return false;
});

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.