MouseEvent.button is a read-only number that tells you which mouse button was pressed or released to cause the event. Learn the value table, when the property is reliable, how it differs from buttons, and five try-it labs.
01
Kind
Instance property
02
Returns
Number (0–4…)
03
Status
Baseline Widely available
04
Access
event.button
05
Best with
mousedown / mouseup
06
Not
buttons bit-field
Fundamentals
Introduction
Left, middle, and right clicks are different actions in many UIs. The browser records which button changed on the event as button—a small integer you can switch on.
Think of button as “which button just changed,” and buttons as “which buttons are held right now” (a bit mask). They use different number systems.
Concept
Understanding the Property
MDN: MouseEvent.button indicates which button was pressed or released on the mouse to trigger the event. It is reliable for press/release-caused events, not for hover/move events like mousemove or mouseenter.
Instance — read event.button in a handler.
Number — usually 0, 1, or 2 for common mice.
Read-only — set it only via constructor options on synthetic events.
Layout note — remapped / left-handed devices may swap physical buttons; 0 still means the main button action.
Foundation
📝 Syntax
JavaScript
const which = mouseEvent.button;
Value
A number representing the button that changed:
button
Meaning (standard layout)
0
Main button (usually left) or uninitialized
1
Auxiliary (usually middle / wheel)
2
Secondary (usually right)
3
Fourth (typically browser Back)
4
Fifth (typically browser Forward)
Compare
⚖️ button vs buttons
button
buttons
Question
Which button changed?
Which buttons are held?
Type
Single index (0, 1, 2…)
Bit-field (1, 2, 4…)
Left
0
1
Right
2
2
Middle
1
4
Reliable for
Press / release events
Broader mouse event types
Do not confuse the two. Left is button === 0 but buttons & 1 for the held mask.
Gotchas
⚠️ When button Is Reliable
Prefer reading button on events that come from pressing or releasing a button, such as mousedown, mouseup, and often click / auxclick / contextmenu.
MDN: it is not reliable for mouseenter, mouseleave, mouseover, mouseout, or mousemove. For those, use buttons if you need held-button state.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Read which button
event.button
Left / main
event.button === 0
Middle
event.button === 1
Right
event.button === 2
Synthetic right
new MouseEvent("mouseup", { button: 2 })
MDN status
Baseline Widely available (Jul 2015)
Snapshot
🔍 At a Glance
Four facts about button.
Kind
instance
On the event
Type
number
0 left, 2 right…
Status
baseline
Widely available
Use on
mouseup
Not mousemove
Hands-On
Examples Gallery
Examples follow MDN MouseEvent.button. Try left, middle, and right clicks in the try-it labs.
📚 Getting Started
Detect left, middle, and right on mouseup.
Example 1 — Switch on button (MDN-style)
Log which button was released; prevent the browser context menu for demos.
mouseup is a press/release event, so e.button is meaningful. preventDefault() on contextmenu keeps the page from opening the OS menu while you test right-click.
MouseEvent.button is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
MouseEvent.button
Reliable which-button-changed index on press/release events. Pair with buttons for held-state on move.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in modern IE versions
Full support
buttonExcellent
Bottom line: Read event.button on mouseup/mousedown; do not rely on it for mousemove.
Wrap Up
Conclusion
event.button answers “which button changed?” on press and release events. Use 0 / 1 / 2 for left / middle / right in a standard layout, and reach for buttons when tracking held state on move.
Assume 0 is always the physical left hardware button
Replace accessible UI with right-click-only actions
Skip contextmenu handling when demoing right-click
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about button
Which-button-changed index for press/release events.
5
Core concepts
🖱️01
Instance
on the event
Kind
🔢02
0 / 1 / 2
left / mid / right
Values
⚠️03
Not on move
use buttons
Gotcha
⚖️04
≠ buttons
different math
Compare
🛡️05
Baseline
widely available
Status
❓ Frequently Asked Questions
It is a read-only number on a mouse event that indicates which button was pressed or released to trigger that event. Common values: 0 main/left, 1 auxiliary/middle, 2 secondary/right, 3 back, 4 forward.
No. MDN marks MouseEvent.button as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard, so no status warning banner is needed.
button tells which button changed for press/release-related events. buttons is a bit-field of which buttons are currently held and can be used more broadly across mouse event types.
MDN says it only guarantees meaning during events caused by pressing or releasing buttons. It is not reliable for mouseenter, mouseleave, mouseover, mouseout, or mousemove.
Not necessarily. Users can remapped devices (for example left-handed layouts). Value 0 means the main button action—usually left in a standard layout—not always the leftmost physical button.
Pass button in the options to new MouseEvent("mouseup", { button: 2 }). Remember button and buttons have different number systems.
Did you know?
Some pointing devices have only one physical button and use keyboard chords or other input to mean “secondary.” The browser still reports the logical button value for the action—so write for the logical main/secondary roles, not only for three-button mice.