How do I use jQuery to get the mouse button I click?
$('div').bind('click', function(){ alert('clicked'); });
This is triggered by the right mouse button and the left mouse button. What is the way to capture the right mouse button? I would be happy if the following existed:
$('div').bind('rightclick', function(){ alert('right mouse button is pressed'); });
#1 building
$.event.special.rightclick = { bindType: "contextmenu", delegateType: "contextmenu" }; $(document).on("rightclick", "div", function() { console.log("hello"); return false; });
#2 building
$("#element").live('click', function(e) { if( (!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1) ) { alert("Left Button"); } else if(e.button == 2){ alert("Right Button"); } });
Update the current state of things:
var $log = $("div.log"); $("div.target").on("mousedown", function() { $log.text("Which: " + event.which); if (event.which === 1) { $(this).removeClass("right middle").addClass("left"); } else if (event.which === 2) { $(this).removeClass("left right").addClass("middle"); } else if (event.which === 3) { $(this).removeClass("left middle").addClass("right"); } });
div.target { border: 1px solid blue; height: 100px; width: 100px; } div.target.left { background-color: #0faf3d; } div.target.right { background-color: #f093df; } div.target.middle { background-color: #00afd3; } div.log { text-align: left; color: #f00; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="target"></div> <div class="log"></div>
#3 building
There are many very good answers, but I just want to make a major difference between event.button IE9 and IE < 9 when using event.button.
According to the old Microsoft event.button specification, this code is different from the code used by W3C. W3C only considers three cases:
- Left click event.button === 1
- Right click event.button === 3
- Middle click event.button === 2
However, in the older Internet Explorer, Microsoft slightly pressed the button, there are eight situations:
- No button clicked event.button === 0 or 000
- Click the left button event.button === 1 or 001
- Right click event.button === 2 or 010
- Click the left and right buttons event.button === 3 or 011
- Click the middle button event.button === 4 or 100
- Click the middle and left keys event.button === 5 or 101
- Middle and right click event.button === 6 or 110
- Click all 3 buttons event.button === 7 or 111
Although this should work in theory, Internet Explorer has never supported pressing two or three buttons at the same time. I mention it because the W3C standard doesn't even support it in theory.
#4 building
$(document).ready(function () { var resizing = false; var frame = $("#frame"); var origHeightFrame = frame.height(); var origwidthFrame = frame.width(); var origPosYGrip = $("#frame-grip").offset().top; var origPosXGrip = $("#frame-grip").offset().left; var gripHeight = $("#frame-grip").height(); var gripWidth = $("#frame-grip").width(); $("#frame-grip").mouseup(function (e) { resizing = false; }); $("#frame-grip").mousedown(function (e) { resizing = true; }); document.onmousemove = getMousepoints; var mousex = 0, mousey = 0, scrollTop = 0, scrollLeft = 0; function getMousepoints() { if (resizing) { var MouseBtnClick = event.which; if (MouseBtnClick == 1) { scrollTop = document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop; scrollLeft = document.documentElement ? document.documentElement.scrollLeft : document.body.scrollLeft; mousex = event.clientX + scrollLeft; mousey = event.clientY + scrollTop; frame.height(mousey); frame.width(mousex); } else { resizing = false; } } return true; } });
#5 building
You can easily tell which mouse button is checked for the properties of the event object that pressed which mouse event:
/* 1 = Left mouse button 2 = Centre mouse button 3 = Right mouse button */ $([selector]).mousedown(function(e) { if (e.which === 3) { /* Right mouse button was clicked! */ } });