Reputation: 1606
So I'm feeling a little confused. I'm working on a HTML5 game project and for a specific menu I want the user to be able to add value to an image through a function using left click but then remove value with right click instead of the menu showing up. I have read about 3 ways to do this:
I've been assigning the event listener to a div, for some reason I can't get if (event.button === 2)
to work, even though I read that was the right mouse button. However the div.oncontextmenu = doRightClick;
works just fine. And then there's event.which
, which I still don't seem to understand. It seems like a global event.button but with keyboard keys included?
My question is, is there a reason to ever use event.button
or event.which
over .oncontextmenu
? Is there any advantages to one over the other? Or browser issues with any? I read that event.button
has different values for IE8 and under which is kinda lame. Any help is greatly appreciated, thanks in advance.
Upvotes: 3
Views: 1667
Reputation: 78840
The contextmenu
event isn't really for right-clicks; it's for context menus, however they're invoked (context menu keyboard key, special click with one-button mice, etc.). You'll probably want to still capture those contextmenu
events so you can cancel them, but then use mousedown
or mouseup
for specifically handling right-clicks.
Upvotes: 1