HTML onmouseup Attribute

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

Introduction

The onmouseup attribute is an inline event handler for the mouseup event. It runs when the user releases a mouse button while the pointer is over an element. That makes it useful for ending press effects, finishing drag operations, or detecting release after onmousedown. It is not the same as onclick, which fires only after a complete press-and-release on the same element. Pass event to read event.button and pointer position. For drags, attach mouseup to document so release outside the element still runs your cleanup code.

What You’ll Learn

01

Event handler

Inline JS.

02

mouseup

Button up.

03

Most elements

Wide support.

04

+ mousedown

Press pair.

05

addEventListener

Preferred way.

06

vs click

Release only.

Purpose of onmouseup Attribute

The primary purpose of onmouseup is to run code when a mouse button is released. Common uses include clearing a “pressed” visual state, stopping a drag, completing a slider handle move, or logging that the user finished a press gesture.

In the typical sequence on one element: mousedown → (optional mousemove) → mouseupclick. If the pointer leaves the element before release, mouseup may fire on a different element — which is why drag code listens on document.

💡
Release ≠ click

onmouseup fires on button release. onclick fires only when down and up happen on the same element without canceling. Use onclick for normal button actions.

📝 Syntax

Set onmouseup on any element that should respond when a mouse button is released:

onmouseup.html
<button type="button" onmouseup="handleMouseUp(event)">
  Release here
</button>

<div
  onmousedown="startPress(this)"
  onmouseup="endPress(this)">
  Press and release
</div>

Syntax Rules

  • Value is JavaScript executed when the mouseup event fires.
  • Works on most elements: button, div, a, etc.
  • Pass event for button, clientX, clientY.
  • Fires after mousedown on the same gesture.
  • Fires before click when both occur on the same element.
  • Use type="button" on buttons inside forms to avoid accidental submit.
  • Modern alternative: el.addEventListener("mouseup", handler).

💎 Values

The onmouseup attribute accepts a string of JavaScript code:

  • onmouseup="handleMouseUp(event)" — Pass the event object.
  • onmouseup="endPress(this)" — Reset the element that received release.
  • JavaScript: btn.onmouseup = (e) => { … } — property assignment.
mouseup-js.html
box.addEventListener("mousedown", () => {
  box.classList.add("pressed");
});

box.addEventListener("mouseup", () => {
  box.classList.remove("pressed");
});

// Drag: listen on document so release anywhere ends drag
document.addEventListener("mouseup", () => {
  dragging = false;
});

⚡ Quick Reference

Property / eventWhen it firesNotes
mousedownButton pressed downonmousedown
mouseupButton releasedonmouseup
clickAfter down + uponclick
event.buttonIn handler0 left, 1 middle, 2 right
Handler attributeonmouseupInline on element

Applicable Elements

TargetSupported?Notes
<button>YesPrefer onclick for actions
<div>, <span>YesDrag handles, custom controls
<a>YesRare; navigation uses click
<input>YesSelection may interfere
document / windowVia JSEnd drag globally

onmouseup vs onmousedown vs onclick

HandlerWhen it firesTypical use
onmousedownButton pressed downStart press, begin drag
onmouseupButton releasedEnd press, stop drag
onclickFull click (down + up)Button actions, toggles
onmousemoveWhile moving overDrag tracking

Examples Gallery

Status on release, addEventListener with event.button, and mousedown + mouseup press state.

👀 Live Preview

Press and release the button — status updates on mouseup:

Status: waiting for mouseup…

Example — Inline onmouseup handler

Show feedback when the mouse button is released:

button-onmouseup.html
<button type="button" onmouseup="handleMouseUp()">
  Release me
</button>
<p id="status"></p>

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

How It Works

mouseup fires the instant the button goes up. The handler runs at that moment.

Dynamic Values with JavaScript

Attach with addEventListener:

dynamic-onmouseup.html
<button type="button" id="dynamicButton">
  Release mouse here
</button>

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

How It Works

Register once in script. The handler receives the event with button and coordinates.

Example — mousedown + mouseup press state

Clear pressed styling when the button is released:

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

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

How It Works

mouseup ends the pressed state. For drags, also listen on document for mouseup outside the box.

♿ Accessibility

  • Prefer onclick for actions — Keyboard users activate with Enter/Space; mouseup alone does not cover keyboard.
  • Use semantic <button> — Not a bare div unless you add keyboard support.
  • Do not rely on mouse-only release — Touch devices use different pointer events.
  • Keep focus visible — Press effects should not remove focus outlines.
  • Provide keyboard alternatives for drag or slider UX built on mouseup.

🧠 How onmouseup Works

1

Button held down

After mousedown.

Press
2

User releases

Button goes up.

Release
3

mouseup fires

onmouseup runs.

Event
=

Cleanup

End drag, unpress.

Browser Support

The mouseup event and onmouseup handler are supported in all browsers, including legacy Internet Explorer.

Mouse Events · Fully supported

Universal onmouseup support

Every major browser fires mouseup when a mouse button is released 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
onmouseup attribute 100% supported

Bottom line: Safe to use for release detection and drag cleanup in any browser.

💡 Best Practices

✅ Do

  • Pair with onmousedown for press-and-release UX
  • Use document.addEventListener("mouseup", …) to end drags
  • Use onclick for standard button actions
  • Pass event to read button and coordinates
  • Prefer addEventListener("mouseup", …) in production

❌ Don’t

  • Confuse mouseup with click — different timing
  • Use alert() for release feedback
  • Put primary actions only on mouseup — keyboard users miss them
  • Assume release fires on the same element as mousedown during drags
  • Forget touch/pointer events on mobile

Conclusion

The onmouseup attribute runs JavaScript when a mouse button is released over an element — ideal for ending press states, stopping drags, and completing pointer gestures.

Pair it with onmousedown, use onclick for normal actions, and listen on document when release may happen outside the target.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onmouseup

Bookmark these before handling pointer release.

5
Core concepts
🔄 02

After mousedown

Same gesture.

Order
🎯 03

End drag

Cleanup.

UX
👉 04

Then click

If same el.

Sequence
05

Use onclick

For actions.

A11y

❓ Frequently Asked Questions

It runs JavaScript when the mouseup event fires — when the user releases a mouse button while the pointer is over the element.
No. mouseup is release only. click requires mousedown and mouseup on the same element without cancellation.
mousedown starts press or drag; mouseup ends it and clears visual state.
Yes for drags — the user may release outside the original element. A document listener ensures cleanup always runs.
addEventListener("mouseup", handler) is preferred for production code.
Yes — universal support including Internet Explorer.

Master mouse release events

Practice the onmouseup attribute with release feedback examples in the Try It editor.

Try onmouseup 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