How do I distinguish between left and right mouse clicks in jQuery?

Asked 2 years ago, Updated 2 years ago, 56 views

$('div').bind('click', function(){
    alert('clicked');
});

$('div').bind('rightclick', function(){ 
    alert('right mouse button is pressed');
});

I want to distinguish between right and left clicks like this, but what do I do...

javascript jquery

2022-09-21 21:22

1 Answers

Use event.which for jQuery

$('#element').mousedown(function(event) {
    switch (event.which) {
        case 1:
            alert('Left Mouse button pressed.');
            break;
        case 2:
            alert('Middle Mouse button pressed.');
            break;
        case 3:
            alert('Right Mouse button pressed.');
            break;
        default:
            alert('You have a strange Mouse!');
    }
});

You can do it like this.


2022-09-21 21:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.