views:

53

answers:

3

I need some way to find out when user released mouse button - something like keyUp - but for mouse ...

Any ideas?

PS: I want to mimic :active pseudo class, and my idea was toDoSomething() on mousePress, and UnDoSomething() on mouseButtonUp ... now ... if you got better solution - hit me up! :)

+1  A: 

The event is onmouseup: http://www.w3schools.com/jsref/event_onmouseup.asp

Or mouseup if you're calling element.addEventListener or using one of the JavaScript frameworks that drop the "on" part of the name.

Update:

If you want to listen for any mouse up event, you'll need to register an onmouseup listener on the page body in your onmousedown callback, and then unregister the onmouseup listener from within the onmouseup callback (so it only happens once).

If you're using jQuery, this can be accomplished with the one() function.

Of course, if the user moves their mouse outside of the browser window before releasing their button, you won't get the mouseup event. I'm not sure if there's anything you can do about that.

pib
Yeah, but that doesn't work for me, because user can release mouse button away from element. mouseUp just works if you are above given element. I need to work something out.
Popara
I am making maximized window application - so basically I don't care out-of-the-window situations. Thanks - you have made some thing clear to me :)
Popara
+1  A: 

The according event is called onmouseup... You can see a sample implementation here: http://www.webmonkey.com/codelibrary/JavaScript_Event_-_Mouseup

Mef
+1  A: 

What you might be looking for is something like:

<button onmouseup="alert('hello world');">Say hello world on mouse-up</button>
CodeMonkey