Bind event to right mouse click

JqueryCss

Jquery Problem Overview


How can I trigger some action with right click after disabling the browser context menu ?

I tried this . . .

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        $('.alert').fadeToggle();
        return false;
    });
});

Jquery Solutions


Solution 1 - Jquery

There is no built-in oncontextmenu event handler in jQuery, but you can do something like this:

$(document).ready(function(){ 
  document.oncontextmenu = function() {return false;};
 
  $(document).mousedown(function(e){ 
    if( e.button == 2 ) { 
      alert('Right mouse button!'); 
      return false; 
    } 
    return true; 
  }); 
});

Basically I cancel the oncontextmenu event of the DOM element to disable the browser context menu, and then I capture the mousedown event with jQuery, and there you can know in the event argument which button has been pressed.

You can try the above example here.

Solution 2 - Jquery

The function returns too early. I've added a comment to the code below:

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        return false;
        $('.alert').fadeToggle(); // this line never gets called
    });
});

Try swapping the return false; with the next line.

Solution 3 - Jquery

Just use the event-handler. Something like this should work:

$('.js-my-element').bind('contextmenu', function(e) {
     e.preventDefault();
     alert('The eventhandler will make sure, that the contextmenu dosn't appear.');
});

Solution 4 - Jquery

I found this answer here and I'm using it like this.

Code from my Library:

$.fn.customContextMenu = function(callBack){
	$(this).each(function(){
        $(this).bind("contextmenu",function(e){
			 e.preventDefault();
			 callBack();
		});
    });	
}

Code from my page's script:

$("#newmagazine").customContextMenu(function(){
    alert("some code");
});

Solution 5 - Jquery

document.oncontextmenu = function() {return false;}; //disable the browser context menu
$('selector-name')[0].oncontextmenu = function(){} //set jquery element context menu 

Solution 6 - Jquery

Is contextmenu an event?

I would use onmousedown or onclick then grab the MouseEvent's button property to determine which button was pressed (0 = left, 1 = middle, 2 = right).

Solution 7 - Jquery

To disable right click context menu on all images of a page simply do this with following:

jQuery(document).ready(function(){
	// Disable context menu on images by right clicking
	for(i=0;i<document.images.length;i++) {
		document.images[i].onmousedown = protect;
	}
});

function protect (e) {
	//alert('Right mouse button not allowed!');
	this.oncontextmenu = function() {return false;};
}

Solution 8 - Jquery

.contextmenu method :-

Try as follows

<div id="wrap">Right click</div>

<script>
$('#wrap').contextmenu(function() {
  alert("Right click");
});
</script>

.mousedown method:-

$('#wrap').mousedown(function(event) {
    
        if(event.which == 3){
            alert('Right Mouse button pressed.');
        }  
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZacView Question on Stackoverflow
Solution 1 - JqueryChristian C. SalvadóView Answer on Stackoverflow
Solution 2 - JqueryBennett McElweeView Answer on Stackoverflow
Solution 3 - JquerySimonView Answer on Stackoverflow
Solution 4 - JqueryGüven AltuntaşView Answer on Stackoverflow
Solution 5 - JqueryStuart BentleyView Answer on Stackoverflow
Solution 6 - JqueryBryanHView Answer on Stackoverflow
Solution 7 - JqueryInforMedicView Answer on Stackoverflow
Solution 8 - JqueryArshid KVView Answer on Stackoverflow