In a click event (attached to document) I want to figure out the target when the user started pressing the mousebutton.
Example:
In that case the code below will return a target
outside the popup, but I want to figure out if it started within the popup.
$(document).click(function(e)
{
// will return the target during *releasing the mouse button*
// but how to get the target where the *mouse press started*
console.log(e.target);
}
Of course I could track this manually by listening to mousedown and saving this within a variable - but I rather have something native, because:
Both Jquery or vanilla JavaScript answers are good to me (but preference vanilla)
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.
click. Triggers after mousedown and then mouseup over the same element if the left mouse button was used. dblclick. Triggers after two clicks on the same element within a short timeframe.
MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.
Mouseup is always firing before click, despite this order.
You could use mousedown together with a mouseup function, and have them both saving their targets to a global variable, then compare them in an if statement.
let downTarget, upTarget;
$(document).mousedown(function(e) {
downTarget = e.target;
});
$(document).mouseup(function(e) {
upTarget = e.target;
});
if(upTarget === downTarget){
*do stuff*
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With