Ben Carp
Ben Carp

Reputation: 26568

How can I trigger the Jquery .on() function when the mouse button is pressed, instead of when it is released.

The Jquery code $(element).on("click", selector, handler) , will trigger the handler function after the mouse button is released from the selected element. Is there a way to trigger the event when the mouse button is pressed?

Upvotes: 3

Views: 143

Answers (2)

You can use .mousedown()

$(document).off("mousedown","#ID").on("mousedown","#ID", function(){
...
});

Upvotes: 0

Jan Somers JanS91
Jan Somers JanS91

Reputation: 362

You can use the .mousedown() Function to achieve what you want:

$( "#target" ).mousedown(function(){});

Or you can use the .on() function and pass 'mousedown' as the action:

$( "#target" ).on('mousedown', function(){});

Upvotes: 6

Related Questions