kenwarner
kenwarner

Reputation: 29120

Bind to right-click with jQuery?

I want to bind a function to a right click. Is this possible with jQuery UI?

Upvotes: 6

Views: 8216

Answers (4)

macropan
macropan

Reputation: 1

$(document).bind('contextmenu',function(){
    return false;
});
$.fn.extend({
    "rightClick": function(fn){
        $(this).mousedown(function(e){
            if (3 == e.which) {
                fn();
            }
        });
    }
});
$(function(){
    $('selector').rightClick(function(){
        // defined your right click event here!
    });
}); 

Upvotes: 0

Douglas FG
Douglas FG

Reputation: 1

Try

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        //your
    });
});

Upvotes: 0

kenwarner
kenwarner

Reputation: 29120

though not listed on http://api.jquery.com/bind/, the 'contextmenu' event seems to work

$('.rightclickable').bind('contextmenu', function() {
    // right-click!
});

Upvotes: 10

James Allardice
James Allardice

Reputation: 165971

Not directly, but you can check which mouse button was pressed in a normal mousedown event handler, with the which property of the event object:

$("#someElem").mousedown(function(e) {
    if(e.which == 3) {
        //Right click!
    }
});

Here's a working example of the above.

Upvotes: 7

Related Questions