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?.
A
mousedown
event is fired when the mouse button is pressed but before it is released.The
click
event is fired after themousedown
andmouseup
events.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.
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
$(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/
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.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, whileonMouseUp
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'sonMouseDown
,onMouseUp
, thenonClick
. 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