👀 Live Preview
Press and hold the button — status updates on mousedown:
Status: waiting for mousedown…

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.
Inline JS.
Button down.
Wide support.
Which button.
Preferred way.
Press vs release.
onmousedown AttributeThe 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) → mouseup → click. If the pointer leaves the element before release, click may not fire — but mousedown already did.
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.
Set onmousedown on any element that should respond to a mouse press:
<button type="button" onmousedown="handleMouseDown(event)">
Press me
</button>
<div onmousedown="startDrag(event)">Drag handle</div>mousedown event fires.button, div, a, input, etc.event to read event.button, clientX, clientY.mouseup and click on the same element.type="button" on buttons inside forms to avoid accidental submit.el.addEventListener("mousedown", handler).onmouseup or document listeners for drag logic.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.btn.onmousedown = (e) => { … } — property assignment.element.addEventListener("mousedown", (event) => {
// 0 = primary (left), 1 = middle, 2 = secondary (right)
if (event.button === 0) {
status.textContent = "Left button pressed";
}
});| Property / event | When it fires | Notes |
|---|---|---|
mousedown | Button pressed down | First in click sequence |
mouseup | Button released | onmouseup |
click | After down + up | onclick |
event.button | In handler | 0 left, 1 middle, 2 right |
| Handler attribute | onmousedown | Inline on element |
| Target | Supported? | Notes |
|---|---|---|
<button> | Yes | Preferred for controls |
<div>, <span> | Yes | Needs keyboard/a11y if interactive |
<a> | Yes | Prefer click for navigation |
<input> | Yes | Text selection may interfere |
window / document | Via JS | Global listeners in script |
onmousedown vs onclick vs onmouseup| Handler | When it fires | Typical use |
|---|---|---|
onmousedown | Button pressed down | Press effect, drag start |
onmouseup | Button released | End drag, release state |
onclick | Full click (down + up) | Button actions, toggles |
oncontextmenu | Right-click menu | Custom context menus |
Inline press feedback, addEventListener with event.button, and mousedown + mouseup press state.
Press and hold the button — status updates on mousedown:
Status: waiting for mousedown…
Run a function when the mouse button is pressed down:
<button type="button" onmousedown="handleMouseDown()">
Press me
</button>
<p id="status"></p>
<script>
function handleMouseDown() {
document.getElementById("status").textContent =
"mousedown: Mouse button pressed!";
}
</script>The browser fires mousedown the instant the button goes down. The inline handler runs your function at that moment.
Attach with addEventListener and read event.button:
<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>Register once in script. The handler receives the event with pointer position and button index.
Show “pressed” while the button is held:
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";
});mousedown starts the pressed state; mouseup ends it. For drags, listen on document for mouseup so release outside the element still clears state.
onclick for actions — Keyboard users activate with Enter/Space on buttons; mousedown alone does not cover keyboard.<button> — Not a bare div unless you add tabindex, roles, and keyboard handlers.oncontextmenu for custom menus, not only mousedown.User presses button.
onmousedown runs.
mouseup → click.
Instant response.
The mousedown event and onmousedown handler are supported in all browsers, including legacy Internet Explorer.
onmousedown supportEvery major browser fires mousedown on pointer press over elements.
Bottom line: Safe to use for press detection and drag starts in any browser environment.
onclick for standard button actionsonmousedown for press effects and drag startsevent to read button, clientX, clientYmouseup on document for drag UXaddEventListener("mousedown", …) in productionalert() for user feedbackThe 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.
onmousedownBookmark these before adding mouse handlers.
Not release.
EventEarlier hook.
Order0, 1, 2.
APICommon use.
UXFor actions.
A11ymousedown event fires — when the user presses a mouse button down over the element.mousedown is press down. click is press down plus release on the same element.event.button: 0 = primary (left), 1 = middle, 2 = secondary (right). For context menus, use oncontextmenu.<button> for clickable controls.addEventListener("mousedown", handler) is preferred for production code.Practice the onmousedown attribute with press feedback examples in the Try It editor.
5 people found this page helpful