Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate click vs mousedown/mouseup

I've read several answers on stackoverflow pertaining to this situation, but none of the solutions are working.

I'm trying to do different things based upon whether a user clicks an element, or holds the mouse down on that element using jQuery.

Is it possible to accomplish this?

like image 460
aperture Avatar asked Sep 24 '12 20:09

aperture


People also ask

What is the difference between click and Mousedown?

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 does Mousedown () mean?

Definition and Usage 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.

Does mouseup fire before click?

Mouseup is always firing before click, despite this order.


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/
like image 50
Anton Baksheiev Avatar answered Sep 26 '22 03:09

Anton Baksheiev


Here's one approach

  1. set a variable to true
  2. make a function that will set it to false when called
  3. have a timer ( setTimeout() ) start counting down on mousedown()
  4. on mouseup, clear the timeout, and check if it the variable is true or false
  5. if it is false, call the function you want to happen on click
  6. In any case, set the variable back to true

This will do what you want. Here's a jsfiddle showing how it might work: http://jsfiddle.net/zRr4s/3/

like image 27
daveyfaherty Avatar answered Sep 25 '22 03:09

daveyfaherty