HTML onmousedown Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onmousedown attribute is an inline event handler for the mousedown event. It runs when a pointing-device button is pressed while the pointer is inside the element — before a full click completes.

Compare with onmouseup (release) and click (press + release on the same element). MDN also notes that pointerdown behaves differently for multi-button mice.

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about July 2015.

Accessibility.

Do not make important actions mouse-only. Provide keyboard (and, when needed, touch/pointer) alternatives so everyone can use the control.

HTML Syntax

syntax.html
<element onmousedown=" script">

script is JavaScript that runs on press. Pass event to read event.button, event.clientX, and event.clientY. Script alternative: element.addEventListener("mousedown", handler).

Attribute Values

The attribute value is a script. Common patterns:

Value / patternDescriptionTry it
handler()Call a named function when any button is pressedTry it
handler(event)Pass the MouseEvent to read button and coordinatesTry it
Press effectChange UI on press; often restore on mouseup / mouseleaveTry it
addEventListener("mousedown")Preferred modern registration in appsTry it

Live Preview

Press a mouse button on the area below. Status shows which button and where (relative to the area). Right-click may also open the browser menu.

demo

Use left, middle, or right button if your device has them.

Press area

Last mousedown: (none)

HTML Examples

Short study snippets. Use Try It Yourself for a live editor.

Example 1 — Log Button and Position

Read event.button and coordinates when the press happens.

HTML
<button type="button" onmousedown="logPress(event)">Press me</button>
<p id="log">Waiting…</p>

<script>
  function logPress(e) {
    var names = ["left", "middle", "right"];
    var name = names[e.button] || ("button " + e.button);
    document.getElementById("log").textContent =
      name + " at " + e.clientX + "," + e.clientY;
  }
</script>
Try It Yourself

How It Works

  1. Press a button while the pointer is over the control.
  2. mousedown fires immediately — you do not need to release yet.
  3. Read e.button — 0 left, 1 middle, 2 right.
  4. Read coordinates — clientX / clientY are relative to the viewport.

Example 2 — Press and Release Feedback

Show “Pressed” on mousedown and restore on mouseup.

HTML
<button
  type="button"
  id="pad"
  onmousedown="setLabel('Pressed')"
  onmouseup="setLabel('Released')"
  onmouseleave="setLabel('Ready')">
  Ready
</button>

<script>
  function setLabel(text) {
    document.getElementById("pad").textContent = text;
  }
</script>
Try It Yourself

How It Works

  1. mousedown updates the label as soon as the press starts.
  2. mouseup restores “Released” when the button comes up.
  3. mouseleave resets if the pointer leaves while held (release may fire elsewhere).
  4. For real “submit” actions, prefer onclick so accidental presses that drag away cancel naturally.

Example 3 — mousedown vs click

Log both events so you can feel the difference in timing.

HTML
<button type="button" id="btn">Press and release</button>
<ul id="log"></ul>

<script>
  var btn = document.getElementById("btn");
  var log = document.getElementById("log");

  function add(line) {
    var li = document.createElement("li");
    li.textContent = line;
    log.appendChild(li);
  }

  btn.addEventListener("mousedown", function () {
    add("mousedown — button down");
  });
  btn.addEventListener("click", function () {
    add("click — press + release on same element");
  });
</script>
Try It Yourself

How It Works

  1. Press down — mousedown logs first.
  2. Release on the same button — click logs after.
  3. Press and drag away before release — you may get mousedown without click.
  4. Prefer addEventListener in apps so multiple handlers can coexist.

Edge Cases & Pitfalls

Most surprises come from treating every press as a completed click, or ignoring keyboard users.

timing

Press is not a click

Users can press, drag off, and release elsewhere — click may never fire. Choose the event that matches your intent.

a11y

Keyboard users skip mousedown

Activating a button with Enter/Space does not fire mousedown the same way. Use real <button> / links and also handle click or keyboard events.

buttons

Every mouse button can fire mousedown

MDN: physical mice fire mousedown for each button. pointerdown only fires for the first button press until all are released.

context

Right-click still fires mousedown

Secondary-button press triggers mousedown and may open the context menu (oncontextmenu).

touch

Touch may synthesize mouse events

Do not assume perfect parity on phones. For multi-device UIs, consider Pointer Events or dedicated touch handling.

Browser Support

onmousedown is an HTML event handler for the mousedown event in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onmousedown

Widely available since about July 2015. Supported across modern desktop and mobile browsers (with the usual mouse-vs-touch caveats).

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer Legacy support
Supported
onmousedown Widely supported

Bottom line: Safe to use for press detection. Prefer click for primary actions; use addEventListener in larger apps.

Key Takeaways

  • Role: run script the moment a pointer button is pressed.
  • vs click: mousedown is press; click is press + release on the same element.
  • API: use event.button and coordinates from the MouseEvent.
  • A11y: never make critical features mouse-only.

One line: use onmousedown for the press moment — use onclick for completed activation.

Frequently Asked Questions

It is an inline event handler that runs JavaScript when the mousedown event fires — a pointing-device button is pressed while the pointer is inside the element.
mousedown fires as soon as the button is pressed. click fires after a full press and release while the pointer stays on the same element. Use click for most button actions; use mousedown when you need the press moment (for example press-and-hold UI).
onmousedown runs on press. onmouseup runs when the button is released. Release can happen on a different element than the press.
On a MouseEvent, button is 0 for the primary (usually left) button, 1 for the auxiliary (usually middle/wheel) button, and 2 for the secondary (usually right) button.
Many browsers synthesize mouse events for taps, but behavior varies. Prefer Pointer Events or also handle touch for reliable multi-device input.
No. The mousedown event is Baseline widely available. Inline handlers are fine for learning; apps often use addEventListener("mousedown", handler).

Next: onmousemove

Track the pointer as it moves over an element.

onmousemove tutorial →

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.

8 people found this page helpful