I am new to jquery
and i have a doubt whether using events.preventDefault()
in the mousedown
or mouseup
events does prevent the click
or dblclick
event?
Please provide me a clarification or a sample.
Thanks in advance. Madhu
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.
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.
The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.
MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.
Neither of mouseup
or mousedown
prevent the default click event.
Fiddle Demo
You need to use click()
:
$('#test').on('click', function(e) {
e.preventDefault();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div onclick="alert('Clicked')" id="test">Click Here</div>
Fiddle Demo
It does not prevent the event itself, but the action that is triggered by the event.
A simple example would be clicking on an anchor link. The default action of the click event is to take the browser to a new URL. In this case, it won't happen.
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