JavaScript MouseEvent button Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

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

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.

JavaScript
element.addEventListener("mouseup", (event) => {
  console.log(event.button); // 0, 1, 2, …
});
💡
Beginner tip

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.

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.

📝 Syntax

JavaScript
const which = mouseEvent.button;

Value

A number representing the button that changed:

buttonMeaning (standard layout)
0Main button (usually left) or uninitialized
1Auxiliary (usually middle / wheel)
2Secondary (usually right)
3Fourth (typically browser Back)
4Fifth (typically browser Forward)

⚖️ button vs buttons

buttonbuttons
QuestionWhich button changed?Which buttons are held?
TypeSingle index (0, 1, 2…)Bit-field (1, 2, 4…)
Left01
Right22
Middle14
Reliable forPress / release eventsBroader mouse event types

Do not confuse the two. Left is button === 0 but buttons & 1 for the held mask.

⚠️ 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.

⚡ Quick Reference

GoalCode
Read which buttonevent.button
Left / mainevent.button === 0
Middleevent.button === 1
Rightevent.button === 2
Synthetic rightnew MouseEvent("mouseup", { button: 2 })
MDN statusBaseline Widely available (Jul 2015)

🔍 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

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.

JavaScript
const button = document.querySelector("#button");
const log = document.querySelector("#log");

button.addEventListener("mouseup", (e) => {
  switch (e.button) {
    case 0:
      log.textContent = "Left button clicked.";
      break;
    case 1:
      log.textContent = "Middle button clicked.";
      break;
    case 2:
      log.textContent = "Right button clicked.";
      break;
    default:
      log.textContent = `Unknown button code: ${e.button}`;
  }
});

button.addEventListener("contextmenu", (e) => {
  e.preventDefault();
});
Try It Yourself

How It Works

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.

Example 2 — Accept Left Button Only

Ignore middle and right for a primary action.

JavaScript
target.addEventListener("mousedown", (e) => {
  if (e.button !== 0) {
    console.log("Ignored non-primary button:", e.button);
    return;
  }
  console.log("Primary (left) press");
});
Try It Yourself

How It Works

Checking e.button === 0 (or !== 0 to bail out) is the usual pattern for “left click only” drag or paint tools.

📈 Synthetic Events, buttons & Pitfalls

Construct events, compare APIs, avoid hover misuse.

Example 3 — Synthetic Event with button: 2

Simulate a secondary-button release in tests.

JavaScript
const event = new MouseEvent("mouseup", {
  button: 2,
  bubbles: true
});

console.log(event.button); // 2
console.log(event.type);   // "mouseup"
Try It Yourself

How It Works

Pass button in MouseEvent() options. Dispatch if listeners must run.

Example 4 — Same Gesture, Different Numbers

Left press: button is 0, held mask buttons is 1.

JavaScript
const leftDown = new MouseEvent("mousedown", {
  button: 0,   // which changed
  buttons: 1   // left currently held (bit 0)
});

const rightDown = new MouseEvent("mousedown", {
  button: 2,
  buttons: 2   // right held (bit 1)
});

console.log(leftDown.button, leftDown.buttons);   // 0 1
console.log(rightDown.button, rightDown.buttons); // 2 2
Try It Yourself

How It Works

Memorize: left is index 0 for button, bit value 1 for buttons. Mixing them up is a common beginner bug.

Example 5 — Prefer buttons on mousemove

MDN: do not trust button on move/hover events.

JavaScript
canvas.addEventListener("mousemove", (e) => {
  // Unreliable for "which button changed":
  // console.log(e.button);

  // Better: are any buttons held while moving?
  const leftHeld = (e.buttons & 1) === 1;
  console.log("dragging with left?", leftHeld);
});
Try It Yourself

How It Works

For drag painting, check e.buttons on mousemove. Use e.button on mousedown / mouseup to know which button started or ended the gesture.

🚀 Common Use Cases

  • Distinguishing left vs right click in custom UI (with care for accessibility).
  • Ignoring non-primary buttons in drawing or drag tools.
  • Detecting middle-click (button 1) for open-in-new-tab style actions.
  • Unit tests that construct MouseEvent with a specific button.
  • Teaching the difference between button and the buttons bit-field.

🔧 How It Works

1

User presses or releases a button

Main, auxiliary, secondary, or extra buttons.

Input
2

Browser builds MouseEvent

Sets button to the changed-button index.

Event
3

Listener on mouseup / mousedown

Your handler receives a reliable button value.

Handler
4

Switch or compare

Branch on 0, 1, 2, and friends.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Unreliable on mousemove / enter / leave / over / out — use buttons there.
  • Value 0 is the main button action, not always the physical leftmost hardware button.
  • Related: altKey, buttons, MouseEvent(), addEventListener(), Window, JavaScript hub.

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in modern IE versions
Full support
button Excellent

Bottom line: Read event.button on mouseup/mousedown; do not rely on it for mousemove.

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.

Continue with buttons, altKey, MouseEvent(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read button on mousedown / mouseup
  • Use a switch for left / middle / right
  • Use buttons for drag state on mousemove
  • Remember left is 0 for button, 1 for buttons
  • Test with synthetic MouseEvent options

❌ Don’t

  • Trust button on mousemove / enter / leave
  • Confuse button indexes with buttons bits
  • Assume 0 is always the physical left hardware button
  • Replace accessible UI with right-click-only actions
  • Skip contextmenu handling when demoing right-click

Key Takeaways

Knowledge Unlocked

Five things to remember about button

Which-button-changed index for press/release events.

5
Core concepts
🔢 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.

Next: MouseEvent.buttons

Learn the held-button bit-field for drag and mousemove.

buttons →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful