Find which mouse button clicked using jQuery

For one of my requirement, I need to determine which mouse button (Left, Middle or Right) was clicked. jQuery provides mousedown() event, using which we can check which mouse button is clicked. For key or button events, event attribute indicates the specific button or key that was pressed. event.which will give 1, 2 or 3 for left, middle and right mouse buttons respectively. The advantage of using event.which is that it eliminates cross browser compatibility.

$(document).ready(function() {
$('#btnClick').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:
           break;
    }
});
});
See live Demo and Code.

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...