15

I've seen plenty of questions and answers on SO and elsewhere that talk about right click events and how to catch and handle them with JavaScript, generally using the .button attribute of the event object generated by the browser.

However, the one thing I haven't found, probably because it's a pretty weird request, is how to catch and handle right clicks on an HTML <button /> element. Buttons are not handled the same way by the browser as other elements. Most importantly, it seems that a right click on a button doesn't do anything. No context menu and, from what I can tell, no event.

Am I wrong? I hope so, because it will make my life easier. If not, I can simulate a button with a div and some CSS, but I'd rather avoid it. Any thoughts?

(PS: This is for a silly side project, so don't worry about me trying to put a button-right-click-based interface in front of any customers or anything. I'm well aware of how horrible that interface would probably be.)

Ken Bellows
  • 6,711
  • 13
  • 50
  • 78
  • Possible duplicate: http://stackoverflow.com/questions/2405771/is-right-click-a-javascript-event – DevlshOne Jul 25 '13 at 13:16
  • "I can* simulate a button with a div" – Ian Clark Jul 25 '13 at 13:16
  • The `contextmenu` event fires for me on Chrome, Firefox, Opera (all on Linux) and on IE10 on Windows 7: http://jsbin.com/oduhak/1 Don't have earlier handy IEs to check (too many VMs running already). – T.J. Crowder Jul 25 '13 at 13:17
  • @DevlshOne not a duplicate. As I mentioned in the first sentence of my post, while I've seen that post and others like it, it doesn't seem to apply for buttons. Though I'll check on the `contextmenu` event as TJCrowder suggested – Ken Bellows Jul 25 '13 at 13:26

4 Answers4

13

http://jsfiddle.net/jqYN5/ is this what you are looking for? Adding context-menu:

<input type="button" value="click me" id="btn">
<button id="btn2">right click</button>`
document.getElementById('btn').onclick = function() {
  alert('click!')
}

document.getElementById('btn2').oncontextmenu = function() {
  alert('right click!')
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
ra_sangeeth
  • 467
  • 3
  • 13
12

Sure, just add a onmousedown listener, check which mouse was pressed:

JS:

document.getElementById("test").onmousedown = function(event) {
    if (event.which == 3) {
        alert("right clicked!");
    }
}

HTML:

<button id="test">Test</button>

Demo: http://jsfiddle.net/Jmmqz/

tymeJV
  • 103,943
  • 14
  • 161
  • 157
3

Or, with perhaps a bit more beginner level understanding:

<script type="text/javascript">
function mouseclick(ele, e) {
if (e.type == 'click') {
    alert('Left Click Detected!');
}
    if (e.type == 'contextmenu') {
    alert('Right Click Detected!');
}
}
</script>

<a href="/" 
onclick="mouseclick(this, event)" 
oncontextmenu="mouseclick(this, event)" >link name</a>
Rusty Nail
  • 2,692
  • 3
  • 34
  • 55
0

You can make use of "context Menu" also :

document.getElementById('btn2').oncontextmenu = function() {
 alert('right click!')
}
Manish Patwari
  • 566
  • 6
  • 20