4

Every click event on the web works on mouse click and release. Is there a way to do a action on a click event before mouse release??

18

Because "mouse click" is defined as down/up in the same spot. On the other hand, down/move/up would not be a mouse click but a mouse drag. If you want to specifically listen for mouse button down, most event systems allow you to subscribe for mouse down event. In JavaScript, that would be the onmousedown event.

  • is it a good practice to use mousedown instead of click event? – Prem Anand Dec 9 '13 at 19:02
  • 4
    personally, I find it poor usability to trigger stuff on mousedown. I expect to be able to change my mind after pressing down on the button. I do this by dragging away before releasing. – MetaFight Dec 9 '13 at 19:06
  • @MetaFight why would someone with the intention of clicking a button will move away after mouse down? – Prem Anand Dec 9 '13 at 19:11
  • 4
    @PremAnand People change their minds, especially when acting hastily or the UI responding surprisingly. You may have misread the label or some other part of the UI. Or maybe even you wanted to click something, but then something else pops up under the mouse. Or you're using a bad mouse/touchpad and mistakenly click the wrong button. And so on, and so forth. – back2dos Dec 9 '13 at 19:30
  • 2
    As DXM mentioned, it's almost instinctual to drag the mouse and release to cancel an inadvertent click. I'm sure many users do this without even realizing it. In fact, I only realized I was doing it when the application I was using didn't allow it (the action was triggered on mouse-down alone). It's jarring. Hence my comment regarding usability. – MetaFight Dec 9 '13 at 20:13
1

In your case, you could trap onmousedown or onmouseup before the click event is fired.

The onmousedown, onmouseup, and onclick events are all parts of a mouse-click. First when a mouse-button is clicked, the onmousedown event is triggered, then, when the mouse-button is released, the onmouseup event is triggered, finally, when the mouse-click is completed, the onclick event is triggered.

Javascript Event Tutorial

1

If you mouse-down on a button, move the mouse off the button surface and finally release the mouse, no mouse click event will be fired. This can save your day, if you have pressed the mouse on the delete-all-my-work-button inadvertently!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.