DOM:event.button
From MDC
Contents |
[edit] Summary
Indicates which mouse button caused the event.
[edit] Syntax
var buttonCode = event.button;
Returns an integer value indicating the button that changed state.
- 0 for standard 'click', usually left button
- 1 for middle button, usually wheel-click
- 2 for right button, usually right-click
The order of buttons may be different depending on how the pointing device has been configured.
[edit] Example
<script type="text/javascript">
function whichButton(e)
{
// Handle different event models
var e = e || window.event;
var btnCode;
if ('object' == typeof e){
btnCode = e.button;
switch (btnCode){
case 0 : alert('Left button clicked');
break;
case 1 : alert('Middle button clicked');
break;
case 2 : alert('Right button clicked');
break;
default : alert('Unexpected code: ' + btnCode);
}
}
}
</script>
<p onmouseup="whichButton(event);" oncontextmenu="event.preventDefault();">Click with mouse...</p>
[edit] Notes
Because mouse clicks are frequently intercepted by the user interface, it may be difficult to detect buttons other than those for a standard mouse click (usually the left button) in some circumstances.
Users may change the configuration of buttons on their pointing device so that if an event's button property is zero, it may not have been caused by the button that is physically left–most on the pointing device; however, it should behave as if the left button was clicked in the standard button layout.
[edit] Specification
DOM 2 Events Specification: button