Reputation: 7612
I am learning events in jquery. While implementing them i came across a doubt. What is the difference between mousedown() and click() event. And which event should i use at what condition.?
For example: Both the events perform the same task in the below code:
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
$("#p1").click(function(){
alert("Mouse down over p1!");
});
Both perform the same.Can someone clarify the difference. If same, which should i prefer?.
Upvotes: 53
Views: 68708
Reputation: 1498
Another main difference (and a very important one to support accessibility):
Click event will get triggered when an element (for example button) has focus and the "Enter" key is pressed
Mousedown event will not get triggered with keyboard action.
Also, mousedown will not work with touch screens.
You will have to add mousedown, keydown, and touchstart events to replicate the click event.
Upvotes: 1
Reputation: 1188
onmousedown + onmouseup = onclick (click event);
** actions ** ** event **
mouse press/down onmousedown
mouse release/up onmouseup
mouse press/down + mouse release/up onclick
Key Enter/space press onclick
Upvotes: 2
Reputation: 1797
onMouseDown
will trigger when either the left or right (or middle) is pressed. Similarly, onMouseUp
will trigger when any button is released. onMouseDown
will trigger even when the mouse is clicked on the object then moved off of it, while onMouseUp
will trigger if you click and hold the button elsewhere, then release it above the object.
onClick
will only trigger when the left mouse button is pressed and released on the same object. In case you care about order, if the same object has all 3 events set, it's onMouseDown
, onMouseUp
, then onClick
. Each even should only trigger once though.
Details:
http://api.jquery.com/click/
http://api.jquery.com/mouseup/
http://api.jquery.com/mousedown/
Source written by Anton Baksheiev
Upvotes: 69
Reputation: 4431
Try this way. Because event.stopPropagation does not stop click event event from mouse down. Mouse down and click events are not related to each other.
var mousedDownFired = false;
$("#id").mousedown(function(event){
mousedDownFired =true;
//code
});
$("#id").click(function(event){
if(mousedDownFired)
{
mousedDownFired = false;
return;
}
//code
});
Updated:
NO . Mouse events are triggered like this way
1) MouseDown
2) Click
3) MouseUp
if mouse down is triggered then flag will enable after mouse down event click will trigger .In this click event will disable the flag variable. this will work as cyclic way. not going to consider two mouse down or two click
Upvotes: 0
Reputation: 10573
onMouseDown
will trigger when either the left or right (or middle) is pressed.
onClick
will only trigger when the left mouse button is pressed and released on the same object.
Upvotes: 1
Reputation: 6908
They do not. You might think so, as you bound both event handlers on the same element, so mousedown will always fire before the click event will occur.
If you bind them on different elements, you will see mousdown will always fire on a button press (any mouse button) without a release and click will fire, after you have released the mouse button of the left (primary) side.
See this small jsfiddle: http://jsfiddle.net/wBfbm/
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
$("#p2").click(function(){
alert("Mouse down over p1!");
});
Upvotes: 1
Reputation: 8715
$(element).click()
fires, when you press mouse button and then release it.
$(element).mousedown()
fires, then you press the mouse button.
Try to hold the clicked button over that button, and then release it here: http://jsfiddle.net/n9rJ9/
Upvotes: 1