Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between mousedown and click in jquery

Tags:

jquery

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?.

like image 644
poorvank Avatar asked Oct 01 '13 06:10

poorvank


People also ask

What is the difference between Mousedown and click?

Note: This differs from the click event in that click is fired after a full click action occurs; that is, the mouse button is pressed and released while the pointer remains inside the same element. mousedown is fired the moment the button is initially pressed.

What is the difference between mouseup and Mousedown?

MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.

What is the function of the Mousedown () method?

jQuery mousedown() Method The mousedown event occurs when the left mouse button is pressed down over the selected element. The mousedown() method triggers the mousedown event, or attaches a function to run when a mousedown event occurs. Tip: This method is often used together with the mouseup() method.

What is Mousedown JavaScript?

JavaScript provides support for various types of events including mouse events. The mousedown is such one kind of mouse event which is supported by JavaScript. The mousedown event can be applied to the element and is triggered whenever the left button of the mouse is pressed over that element.


2 Answers

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

like image 132
pablofiumara Avatar answered Oct 23 '22 00:10

pablofiumara


A mousedown event is fired when the mouse button is pressed but before it is released.

The click event is fired after the mousedown and mouseup events.

like image 21
Jivings Avatar answered Oct 23 '22 00:10

Jivings