Reputation: 1725
Using jQuery, I often like to use mousedown
and mouseup
events in conjunction for pushable buttons.
However, in every case I've used the mouseup
event, binding the click
event instead seemed to produce identical results.
Is there any substantial difference between the two methods below?
// Method 1
$('.myButton').bind('click', callback);
// Method 2
$('.myButton').bind('mouseup', callback);
Please note I'm seeking a technical explanation on the differences between using both methods. This has no relation to the question that has been flagged as a dupe: Differentiate click vs mousedown/mouseup
Upvotes: 51
Views: 47371
Reputation: 1226
The biggest difference that affects the way I code is: the click
event on an a
HTML tag is responsible for changing the URL. In contrast, the mousedown
and mouseup
events will not acheive this
Upvotes: 0
Reputation: 1541
I think Mouse Down and Mouse Up events give you further control over the click event. It divides the click event into two more events so that more details can be coded for each event. Click event restricts the mouse click and force you to code both the events in the same function.
You can understand this restriction if you ever try to make your own dragging behavior.
However you can use click event too if we could change the way how people drag the objects. For example click-1 starts the drag and click-2 stops the drag and puts the object on another position. But there are two problems I see:
Upvotes: 2
Reputation: 130670
I would like to add to the other answers that click
event works on touch-enabled devices while mouseup
/ mousedown
do not (obviously because there's no "mouse")
Note that there's a 300ms delay on touch devices with the click
event.
Upvotes: 1
Reputation: 6281
My understanding is that "click" hides lots of complexities (such as making sure that mousedown/up occur on the same element, cancelling with ESC/right click). Using "click" over "mousedown/up" should be preferred.
One scenario where "click" does not seem to work when app updates content very often in such a way that underlying DOM elements get replaced. In this case "click" will not be triggered and it might result in poor customer experience.
Upvotes: 3
Reputation: 82006
With a mouseup event, you can click somewhere else on the screen, hold down the click button, and move the pointer to your mouseup element, and then release the mouse pointer.
A click event requires the mousedown and mouseup event to happen on that element.
The normal expectation is that a click requires both the mousedown and mouseup event, so I'd recommend the click event.
From the possible duplicate, it appears that mouseup and mousedown events can also be caused by mouse buttons other than the left click button. Which is very different from what a generic user would expect.
Upvotes: 96