HTML onmousedown Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

The onmousedown attribute is an inline event handler for the mousedown event. It runs when the user presses a mouse button down over an element — before they release it. That makes it useful for press effects, starting drag operations, or reacting the instant a button is held. It is not the same as onclick, which fires only after a full press-and-release. Pass event to read event.button (left, middle, or right). For most button actions, prefer onclick; use onmousedown when you need the earliest pointer-down moment.

What You’ll Learn

01

Event handler

Inline JS.

02

mousedown

Button down.

03

Most elements

Wide support.

04

event.button

Which button.

05

addEventListener

Preferred way.

06

vs click

Press vs release.

Purpose of onmousedown Attribute

The primary purpose of onmousedown is to run code at the start of a mouse interaction — when the button goes down, not when the click completes. Common uses include visual “pressed” styling, initiating drag-and-drop, drawing tools, or slider handles that track while the button is held.

The typical mouse sequence on one element is: mousedown → (optional mousemove) → mouseupclick. If the pointer leaves the element before release, click may not fire — but mousedown already did.

💡
Press ≠ click

onmousedown fires on press. onclick fires after press and release on the same element. Use onclick for normal button actions; onmousedown for press-and-hold or drag starts.

📝 Syntax

Set onmousedown on any element that should respond to a mouse press:

onmousedown.html
<button type="button" onmousedown="handleMouseDown(event)">
  Press me
</button>

<div onmousedown="startDrag(event)">Drag handle</div>

Syntax Rules

  • Value is JavaScript executed when the mousedown event fires.
  • Works on most elements: button, div, a, input, etc.
  • Pass event to read event.button, clientX, clientY.
  • Fires before mouseup and click on the same element.
  • Use type="button" on buttons inside forms to avoid accidental submit.
  • Modern alternative: el.addEventListener("mousedown", handler).
  • Pair with onmouseup or document listeners for drag logic.

💎 Values

The onmousedown attribute accepts a string of JavaScript code:

  • onmousedown="handleMouseDown(event)" — Pass the event object.
  • onmousedown="handleMouseDown()" — Works if the function does not need event data.
  • JavaScript: btn.onmousedown = (e) => { … } — property assignment.
event-button.html
element.addEventListener("mousedown", (event) => {
  // 0 = primary (left), 1 = middle, 2 = secondary (right)
  if (event.button === 0) {
    status.textContent = "Left button pressed";
  }
});

⚡ Quick Reference

Property / eventWhen it firesNotes
mousedownButton pressed downFirst in click sequence
mouseupButton releasedonmouseup
clickAfter down + uponclick
event.buttonIn handler0 left, 1 middle, 2 right
Handler attributeonmousedownInline on element

Applicable Elements

TargetSupported?Notes
<button>YesPreferred for controls
<div>, <span>YesNeeds keyboard/a11y if interactive
<a>YesPrefer click for navigation
<input>YesText selection may interfere
window / documentVia JSGlobal listeners in script

onmousedown vs onclick vs onmouseup

HandlerWhen it firesTypical use
onmousedownButton pressed downPress effect, drag start
onmouseupButton releasedEnd drag, release state
onclickFull click (down + up)Button actions, toggles
oncontextmenuRight-click menuCustom context menus

Examples Gallery

Inline press feedback, addEventListener with event.button, and mousedown + mouseup press state.

👀 Live Preview

Press and hold the button — status updates on mousedown:

Status: waiting for mousedown…

Example — Inline onmousedown handler

Run a function when the mouse button is pressed down:

button-onmousedown.html
<button type="button" onmousedown="handleMouseDown()">
  Press me
</button>
<p id="status"></p>

<script>
  function handleMouseDown() {
    document.getElementById("status").textContent =
      "mousedown: Mouse button pressed!";
  }
</script>
Try It Yourself

How It Works

The browser fires mousedown the instant the button goes down. The inline handler runs your function at that moment.

Dynamic Values with JavaScript

Attach with addEventListener and read event.button:

dynamic-onmousedown.html
<button type="button" id="dynamicButton">
  Dynamic press
</button>

<script>
  document.getElementById("dynamicButton").addEventListener("mousedown", function (event) {
    document.getElementById("log").textContent =
      "Button " + event.button + " pressed";
  });
</script>
Try It Yourself

How It Works

Register once in script. The handler receives the event with pointer position and button index.

Example — mousedown + mouseup press state

Show “pressed” while the button is held:

press-state.html
const box = document.getElementById("box");

box.addEventListener("mousedown", () => {
  box.classList.add("pressed");
  box.textContent = "Pressed…";
});

box.addEventListener("mouseup", () => {
  box.classList.remove("pressed");
  box.textContent = "Release me";
});
Try It Yourself

How It Works

mousedown starts the pressed state; mouseup ends it. For drags, listen on document for mouseup so release outside the element still clears state.

♿ Accessibility

  • Prefer onclick for actions — Keyboard users activate with Enter/Space on buttons; mousedown alone does not cover keyboard.
  • Use semantic <button> — Not a bare div unless you add tabindex, roles, and keyboard handlers.
  • Do not rely on mouse only — Touch devices fire different pointer events; test on mobile.
  • Avoid blocking context menu — Right-click uses button 2; use oncontextmenu for custom menus, not only mousedown.
  • Visible focus states — Press effects should not remove focus outlines.

🧠 How onmousedown Works

1

Pointer over element

User presses button.

Input
2

mousedown fires

onmousedown runs.

Event
3

User releases

mouseup → click.

Sequence
=

Press feedback

Instant response.

Browser Support

The mousedown event and onmousedown handler are supported in all browsers, including legacy Internet Explorer.

Mouse Events · Fully supported

Universal onmousedown support

Every major browser fires mousedown on pointer press over elements.

100% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Fully supported
Full support
Opera Fully supported
Full support
onmousedown attribute 100% supported

Bottom line: Safe to use for press detection and drag starts in any browser environment.

💡 Best Practices

✅ Do

  • Use onclick for standard button actions
  • Use onmousedown for press effects and drag starts
  • Pass event to read button, clientX, clientY
  • Pair with mouseup on document for drag UX
  • Prefer addEventListener("mousedown", …) in production

❌ Don’t

  • Confuse mousedown with click — they fire at different times
  • Use alert() for user feedback
  • Put primary actions only on mousedown — keyboard users miss them
  • Block right-click without a good reason
  • Forget touch/pointer events on mobile devices

Conclusion

The onmousedown attribute runs JavaScript the moment a mouse button is pressed over an element — ideal for press feedback, drag starts, and early pointer handling.

For normal clicks use onclick. Pass the event object, prefer addEventListener, and pair with mouseup when you need press-and-hold behavior.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onmousedown

Bookmark these before adding mouse handlers.

5
Core concepts
🔄 02

Before click

Earlier hook.

Order
🔢 03

event.button

0, 1, 2.

API
🎯 04

Drag start

Common use.

UX
05

Use onclick

For actions.

A11y

❓ Frequently Asked Questions

It runs JavaScript when the mousedown event fires — when the user presses a mouse button down over the element.
No. mousedown is press down. click is press down plus release on the same element.
Check event.button: 0 = primary (left), 1 = middle, 2 = secondary (right). For context menus, use oncontextmenu.
Yes, but interactive divs need keyboard support and ARIA. Prefer <button> for clickable controls.
addEventListener("mousedown", handler) is preferred for production code.
Yes — universal support including Internet Explorer.

Master mouse event handlers

Practice the onmousedown attribute with press feedback examples in the Try It editor.

Try onmousedown demo →

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